• Resolved cannibalsmith

    (@cannibalsmith)


    In functions.php of my custom theme:

    function custom_gallery($attr) { /* .. */ }
    add_action('post_gallery', 'custom_gallery');

    The $attr is always an empty string. But it should be an array. When I look at the wp-includes/media.php line 597:

    function gallery_shortcode($attr) {
    global $post;
    // Allow plugins/themes to override the default gallery template.
    $output = apply_filters('post_gallery', '', $attr);

    It seems to be a bug. But then again…

    What to do? How do I obtain proper $attr value?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The $attr value comes from the gallery shortcode. If you look around 10 lines further down in wp-includes/media.php, you’ll see:

    extract(shortcode_atts(array(
    		'order'      => 'ASC',
    		'orderby'    => 'menu_order ID',
    		'id'         => $post->ID,
    		'itemtag'    => 'dl',
    		'icontag'    => 'dt',
    		'captiontag' => 'dd',
    		'columns'    => 3,
    		'size'       => 'thumbnail'
    	), $attr));

    So your custom_gallery function could use exactly the same extract function but you can now insert your own gallery ordering and markup etc. I use a similar custom gallery function to output the final gallery as an unordered list and stop the gallery CSS from being rendered within the <body> of a page (which is a bug).

    The current gallery code in WP is correct. You should be using the filter hook to customize the gallery:

    add_filter( 'post_gallery', 'my_gallery_shortcode', 10, 2 );
    
    function my_gallery_shortcode( $output, $attr ) {
    
    	/* Custom gallery shortcode. */
    
    }
    Thread Starter cannibalsmith

    (@cannibalsmith)

    Thanks, greenshady. My mistake was not passing $accepted_args to add_filter.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Bug? post_gallery filter parameter always empty string’ is closed to new replies.