2

Is there any possibility to make a functions hook, which will automatically override titles of added images (or added during gallery creation) with post title? Just like in the example: - im creating a post called lorem ipsum - I'm uploading a file called pingpong.jpg - without hook by default wordpress will give a title to the image: pingpong.jpg - expected outcome of the hook: after uploading the image my image title will be: lorem ipsum

is it even possible via functions?

on the top of that I would like to utilize automatiacally adding attribute to the gallery shortcode which will handle the change. So far I have something like that:

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;
}
3

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:

[gallery parent_titles="yes" ids="132,321,213"]

[gallery parent_titles="true" ids="132,321,213"]

[gallery parent_titles="1" ids="132,321,213"]

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:

[gallery ids="132,321,213"]

that showed up with the image captions like:

normal

Then I added the custom attribute:

[gallery parent_titles="yes" ids="132,321,213"]

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 ;-)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.