Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use post_gallery filter in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
January 10, 2023
5 minutes read

post_gallery filter

Filters the default gallery shortcode output.

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

Description

This hook can be used to filter the output of the default gallery shortcode.

We can use the post_gallery filter to modify or 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 Examples

Example 1: Basic Usage of post_gallery Filter

To run the hook, copy the example below.

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

Example 2: Adding a Hook Callback

The following example is for adding a hook callback.

// define the post_gallery callback 

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

// add the filter 
add_filter( 'weplugins_post_gallery', 'weplugins_filter_post_gallery', 10, 3 ); 

Example 3: Removing a Hook Callback

To remove a hook callback, use the example below.

// remove the filter 
remove_filter( 'weplugins_post_gallery', 'weplugins_filter_post_gallery', 10, 3 ); 

Contact Us

If you need customization, feel free to Contact Us.

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.