Gallery images titles – get from post

Gallery Image Caption – As Title Of The Post It’s attached To

Here’s one way to do it with a custom parent_titles attribute in the native gallery shortcode.

This can be achieved by setting suppress_filters to false for the gallery query and modify the posts excerpts through the the_posts filter. We can then check for the custom attribute inputs through the shortcode_atts_gallery filter.

Example

Using parent_titles as a boolean-string we could use it like shown here:






Here are some screenshots from the testcase I created for @JuliaGalden

First I created three posts and attached a corresponding color image to each one:

posts

Then I created a gallery with:


that showed up with the image captions like:

normal

Then I added the custom attribute:


and it showed up like:

With parent titles

where the captions are now the titles from the parent posts.

Demo plugin

Here’s a demo plugin that could support this feature (PHP 5.4+):

<?php
/** 
 * Plugin Name: Gallery Image Caption As The Parent Post's Title
 * Description: Support for boolean parent_titles attribute in native post galleries
 * Plugin URI:  http://wordpress.stackexchange.com/a/228857/26350
 * Version:     1.0.1
 */

namespace WPSE\Q228851;

class Main 
{
    /**
     * @var bool
     */
    private $active;    

   /**
    * Setup actions and filters
    */
    public function activate()
    {
       add_filter( 'shortcode_atts_gallery', [ $this, 'shortcode_atts_gallery' ], 999, 3 );
       add_action( 'pre_get_posts',          [ $this, 'pre_get_posts' ] );
       add_filter( 'the_posts',              [ $this, 'the_posts' ] );      
    }   

   /**
    * Activate if the parent_titles attribute is set
    */
   public function shortcode_atts_gallery( $out, $pair, $atts )
   {
       if(    isset( $out['parent_titles'] ) 
              && wp_validate_boolean( $out['parent_titles'] )
          || 
              isset( $atts['parent_titles'] ) 
              && wp_validate_boolean( $atts['parent_titles'] )  
       )
           $this->active = true;

        return $out;
    }

   /**
    * Don't suppress filters for the gallery posts query
    */
    public function pre_get_posts( \WP_Query $q )
    {
        if( $this->active )
            $q->set( 'suppress_filters', false );
    }

   /**
    * Override each image title with the title of post it's attached to
    */
    public function the_posts( $posts )
    {
        if( $this->active )
        {
            foreach( $posts as $post )
            {
                if( $post->post_parent )
                    $post->post_excerpt = get_post_field( 'post_title', $post->post_parent ); 
            }
            $this->active = false;
        }
        return $posts;
    }   

} // end class


/**
 * Activate
 */
( new Main )->activate();

How to install: Copy this code into the /wp-content/plugins/galleries-with-parent-post-titles/plugin.php file and activate the plugin in the wp-admin backend the usual way. Then add the parent_titles="yes" to your gallery shortcode where you want to display the parent post titles.

Auto activation for all galleries

To do this automatically for all galleries we can change the Main::shortcode_atts_gallery() method to:

   public function shortcode_atts_gallery( $out, $pair, $atts )
   {
        $this->active = true;
        return $out;
    }

or use the following filter created by @JuliaGalden, that one can test e.g. in functions.php:

add_filter('shortcode_atts_gallery', 'overwrite_gallery_atts_wpse_95965', 10, 3 );
function overwrite_gallery_atts_wpse_95965( $out, $pairs, $atts )
{
    $out['parent_titles'] = 'yes';
    return $out;
}

If we want to use it in the plugin then we have to change it to

add_filter( 'shortcode_atts_gallery', __NAMESPACE__ . '\\overwrite_gallery_atts_wpse_95965', 10, 3 );
function overwrite_gallery_atts_wpse_95965( $out, $pairs, $atts )
{
    $out['parent_titles']='yes';
    return $out;
}

since we added a namespace for our plugin.

We could also create a filter instead:

$this->active = apply_filters( 'wpse_gallery_parent_titles', true, $atts, $pair );

… etc, etc, but I will leave it here for now 😉

Leave a Comment