Support » Plugin: Media Library Assistant » 'Inserted In' and 'Parent ID'

  • Resolved mokummusic

    (@mokummusic)


    Hi David

    I hope you can point me in the right direction with a problem I have. My site uses bbpress and I allow the subscriber role to have media upload permissions for forum posts. I seem to have all that working, but have an issue with the Parent ID of an attachment when it is uploaded into a new topic.
    The Parent ID is set to the page where the topic editor form is, rather than being set to the topic’s ID. I suppose this is to be expected as the topic doesn’t exist when the media uploads into the editor.
    Afterwards, I notice on my MLA page in admin, that the topic ID is available for these uploads in the ‘inserted in’ column.

    Can I retrieve this information to form a link within the MLA gallery shortcode? I use the MLA gallery shortcode on members pages to display all of their uploaded pictures. I’d like to link the images to the parent ID to view them in context of where they were uploaded to.

    I ‘do_shortcode’ in my php:
    [mla_gallery author='.$user_id.' mla_caption="Posted in: <a href={+site_url+}/?p={+parent+}\>{+parent_title+}</a>" mla_rollover_text="Image name - {+name_only+}" link="file" size="medium"]

    What I’d like to achieve:
    Ideally – to replace the parent ID with the topic ID of any attachments once the topic is submitted.
    Alternatively – to link to ‘inserted in’ rather than ‘parent post’ in my gallery shortcode.

    Thank you for your exemplary support!
    Neil

    https://wordpress.org/plugins/media-library-assistant/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thanks for another good question based on your bbPress application; it’s a question that would apply to any site.

    The simplest, most general approach seems to be your second choice – replacing the caption for [mla_gallery] items that are inserted in posts/pages. I have added a new example to the mla_gallery_item_values filter in the mla-hooks-example.php example plugin to accomplish that. Here’s the new code:

    /*
     * For the fifth example, we compose a caption with "Inserted in" links.
     */
    if ( 'show post inserts' == self::$shortcode_attributes['my_filter'] ) {
         // You can use MLAOptions::mla_get_data_source() to get anything available.
        $my_setting = array(
            'data_source' => 'inserted_in',
            'option' => 'raw'
        );
        $inserted_in = MLAOptions::mla_get_data_source( $item_values['attachment_ID'], 'single_attachment_mapping', $my_setting, NULL );
    
        if ( ' ' != $inserted_in ) {
            /*
             * Break the information down:
             * matches[1] => post/page Title
             * matches[2] => post/page post_type
             * matches[3] => post/page ID
             */
            $my_posts = array();
            foreach ( (array) $inserted_in as $insert ) {
                if ( preg_match( '/(.*) \(([^ ]*) (\d+)\)/', $insert, $matches ) ) {
                    // index on post ID to remove duplicates
                    $my_posts[ $matches[3] ] = $matches[1];
                } // match
            } // each insert
    
            // Build the replacement caption
            $my_caption = NULL;
            foreach ( (array) $my_posts as $ID => $title ) {
                if ( empty( $my_caption ) ) {
                    $my_caption = sprintf( 'Posted in: <a href="%1$s/?p=%2$d">%3$s</a>', $item_values['site_url'], $ID, $title );
                } else {
                    $my_caption .= sprintf( ', <a href="%1$s/?p=%2$d">%3$s</a>', $item_values['site_url'], $ID, $title );
                }
            } // each post
    
            if ( ! empty( $my_caption ) ) {
                $item_values['caption'] = $my_caption;
            }
        } // has inserts
    }

    Since you’re already working in PHP you can simply add the filter function and the add_filter( 'mla_gallery_item_values', 'mla_gallery_item_values_filter', 10, 1 ); statement to your existing file. Let me know if you need help with that.

    I am marking this topic resolved, but lease update it if you have any problems with the code or further questions about using the filter to modify your gallery display. Thanks for your interest in the plugin and especially for the positive review you posted!

    Thread Starter mokummusic

    (@mokummusic)

    Thanks once again for your help! I’ve added the following to my theme’s functions.php file, but my mla-gallery shortcode (see my first post) then hangs the page that it is included on (no sidebars etc). Do I need to change any shortcode parameters?

    Here’s the code in my functions.php file:

    function mla_gallery_caption_links_filter () {
    
    /* David's example on wordpress.org support forum
    /* - we compose a caption with "Inserted in" links.
     */
    
    if ( 'show post inserts' == self::$shortcode_attributes['my_filter'] ) {
         // You can use MLAOptions::mla_get_data_source() to get anything available.
        $my_setting = array(
            'data_source' => 'inserted_in',
            'option' => 'raw'
        );
        $inserted_in = MLAOptions::mla_get_data_source( $item_values['attachment_ID'], 'single_attachment_mapping', $my_setting, NULL );
    
        if ( ' ' != $inserted_in ) {
            /*
             * Break the information down:
             * matches[1] => post/page Title
             * matches[2] => post/page post_type
             * matches[3] => post/page ID
             */
            $my_posts = array();
            foreach ( (array) $inserted_in as $insert ) {
                if ( preg_match( '/(.*) \(([^ ]*) (\d+)\)/', $insert, $matches ) ) {
                    // index on post ID to remove duplicates
                    $my_posts[ $matches[3] ] = $matches[1];
                } // match
            } // each insert
    
            // Build the replacement caption
            $my_caption = NULL;
            foreach ( (array) $my_posts as $ID => $title ) {
                if ( empty( $my_caption ) ) {
                    $my_caption = sprintf( 'Posted in: <a href="%1$s/?p=%2$d">%3$s</a>', $item_values['site_url'], $ID, $title );
                } else {
                    $my_caption .= sprintf( ', <a href="%1$s/?p=%2$d">%3$s</a>', $item_values['site_url'], $ID, $title );
                }
            } // each post
    
            if ( ! empty( $my_caption ) ) {
                $item_values['caption'] = $my_caption;
            }
        } // has inserts
    }
    }
    
    add_filter( 'mla_gallery_item_values', 'mla_gallery_caption_links_filter', 10, 1 );
    Plugin Author David Lingren

    (@dglingren)

    Thanks for the update and for posting your source code. I can see a few minor issues that might be causing the problems you’re experiencing. I regret any confusion I caused by posting an excerpt of my code without all the context.

    First, you need to add an $item_values parameter to your function. That’s how the information is passed in to the filter for your use.

    Second, you should remove the first if ( 'show post inserts' == self::$shortcode_attributes['my_filter'] ) test, since your function is not part of a larger example.

    Third, you have to add a return $item_values; statement at the end of the function. That’s how the updated information is passed back from the filter.

    Finally, I would suggest moving the add_filter() call to just before the do_shortcode() function in your main PHP file and adding a remove_filter() call just after it. That way, you code only runs when the shortcode you want to change needs it and any other [mla_gallery] shortcodes will not be affected.

    Here’s an updated copy of your filter with the suggested changes:

    function mla_gallery_caption_links_filter ( $item_values ) {
    
    	/* David's example on wordpress.org support forum
    	 * - we compose a caption with "Inserted in" links.
    	 */
    
         // You can use MLAOptions::mla_get_data_source() to get anything available.
        $my_setting = array(
            'data_source' => 'inserted_in',
            'option' => 'raw'
        );
        $inserted_in = MLAOptions::mla_get_data_source( $item_values['attachment_ID'], 'single_attachment_mapping', $my_setting, NULL );
    
        if ( ' ' != $inserted_in ) {
            /*
             * Break the information down:
             * matches[1] => post/page Title
             * matches[2] => post/page post_type
             * matches[3] => post/page ID
             */
            $my_posts = array();
            foreach ( (array) $inserted_in as $insert ) {
                if ( preg_match( '/(.*) \(([^ ]*) (\d+)\)/', $insert, $matches ) ) {
                    // index on post ID to remove duplicates
                    $my_posts[ $matches[3] ] = $matches[1];
                } // match
            } // each insert
    
            // Build the replacement caption
            $my_caption = NULL;
            foreach ( (array) $my_posts as $ID => $title ) {
                if ( empty( $my_caption ) ) {
                    $my_caption = sprintf( 'Posted in: <a href="%1$s/?p=%2$d">%3$s</a>', $item_values['site_url'], $ID, $title );
                } else {
                    $my_caption .= sprintf( ', <a href="%1$s/?p=%2$d">%3$s</a>', $item_values['site_url'], $ID, $title );
                }
            } // each post
    
            if ( ! empty( $my_caption ) ) {
                $item_values['caption'] = $my_caption;
            }
        } // has inserts
    
    	return $item_values;
    }
    
    add_filter( 'mla_gallery_item_values', 'mla_gallery_caption_links_filter', 10, 1 );

    Let me know if you have any questions about the changes I’ve suggested.

    Thread Starter mokummusic

    (@mokummusic)

    Thanks David! It works great! I had to add one extra line, as I was getting empty titles for certain items:

    foreach ( (array) $my_posts as $ID => $title ) {
            if ($title == '') $title = bbp_get_topic_title($ID);
                if ( empty( $my_caption ) ) {

    Could you give me an example of how I remove the filter after this function runs?

    Cheers!
    Neil

    Plugin Author David Lingren

    (@dglingren)

    Thank you for taking the time to post this update with the good news and the suggested improvement. I’m glad to hear the filter is working for you.

    As I said, the best way to use the filter is to put the “add” and “remove” calls just around the “do_shortcode” statements that require it. Here’s an example:

    add_filter( 'mla_gallery_item_values', 'mla_gallery_caption_links_filter', 10, 1 );
    echo do_shortcode( '[mla_gallery author=' . $user_id . ' mla_caption="Posted in: <a href={+site_url+}/?p={+parent+}\>{+parent_title+}</a>" mla_rollover_text="Image name - {+name_only+}" link="file" size="medium"]' );
    remove_filter( 'mla_gallery_item_values', 'mla_gallery_caption_links_filter', 10 );

    As you can see, the “remove_filter” call is like the “add_filter” call, but with the final argument removed.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘'Inserted In' and 'Parent ID'’ is closed to new replies.