post_gallery filter

Filters the default gallery shortcode output.

 apply_filters( 'post_gallery', string $output, array $attr, int $instance ) 

 

Descrption

This hook can be used for filters the output of default gallery shotcode.

We can use the post_gallery filter to modify/replace the default WordPress gallery template to suit our needs.

If the filtered output isn’t empty, it will be used instead of generating the default gallery template.

Parameters

  • $output : (string) The gallery output. Default empty.
  • $attr : (array) Attributes of the gallery shortcode.
  • $instance : (int) Unique numeric ID of this gallery shortcode instance.

Live Example

To run the hook, copy the example below.

$output = apply_filters( 'post_gallery', $output, $attr, $instance ); 
                         
if ( !empty( $output ) ) { 
                         
   // everything has led up to this point... 
                         
} 
  

The following example is for adding a hook callback.

// define the post_gallery callback 

function filter_post_gallery( $output, $attr, $instance) { 
// make filter magic happen here... 
return $output;
 };

// add the filter 
add_filter( 'post_gallery', 'filter_post_gallery', 10, 3 ); 

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'post_gallery', 'filter_post_gallery', 10, 3 ); 

 

JOIN 100,000+ SUBSCRIBER's FAMILY

 Subscribe to our weekly newsletter below and never miss the latest updates in WordPress