Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following custom function to replace the gallery_shortcode function from core. It hooks in at the beginning and works.

The problem is that I can't get my custom key/values from the gallery shortcode in there. I receive them in the core function, but they don't get passed into my custom function. I can't really investigate what's going wrong or where they get missing, because i don't get any print_r output for $attr inside my custom function. Any ideas?

Note: I added comments for everything I changed from the core to my custom function.

function modified_post_gallery( $attr ) 
{
    // I can't get any output from the following
    echo '<pre>'; 
            print_r( $attr ); 
    echo '</pre>';
    // But: i get the expected output if i place the same line inside the core function: 
    /root/wp-include/media.php line 759 and get the actual output including all custom key/values.

    global $post, $wp_locale;

    static $instance = 0;
    $instance++;

    $output = '';

    // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
    if ( isset( $attr['orderby'] ) ) 
    {
        $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
        if ( ! $attr['orderby'] )
            unset( $attr['orderby'] );
    }

    // extract result from user input & defaults
    extract( shortcode_atts( array(
         'order'        => 'ASC'
        ,'orderby'      => 'menu_order ID'
        ,'id'           => $post->ID
        ,'itemtag'      => 'span' // CHANGED
        ,'icontag'      => 'span' // CHANGED
        ,'captiontag'   => 'span' // CHANGED
        ,'columns'      => 3
        ,'size'         => 'thumbnail'
        ,'include'      => ''
        ,'exclude'      => ''
        ,'class'        => '' // ADDED
        ,'test'
    ), $attr ) );

    $id = intval( $id );
    if ( 'RAND' == $order )
        $orderby = 'none';

    if ( !empty( $include ) ) 
    {
        $include = preg_replace( '/[^0-9,]+/', '', $include );
        $_attachments = get_posts( array( 
             'include'          => $include
            ,'post_status'      => 'inherit'
            ,'post_type'        => 'attachment'
            ,'post_mime_type'   => 'image'
            ,'order'            => $order
            ,'orderby'          => $orderby 
        ) );

        $attachments = array();
        foreach ( $_attachments as $key => $val ) 
        {
            $attachments[$val->ID] = $_attachments[$key];
        }
    } 
    elseif ( !empty( $exclude ) ) 
    {
        $exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
        $attachments = get_children( array( 
             'post_parent'      => $id
            ,'exclude'          => $exclude
            ,'post_status'      => 'inherit'
            ,'post_type'        => 'attachment'
            ,'post_mime_type'   => 'image'
            ,'order'            => $order
            ,'orderby'          => $orderby 
        ) );
    } 
    else 
    {
        $attachments = get_children( array( 
             'post_parent'      => $id
            ,'post_status'      => 'inherit'
            ,'post_type'        => 'attachment'
            ,'post_mime_type'   => 'image'
            ,'order'            => $order
            ,'orderby'          => $orderby 
        ) );
    }

    // No attachments, so abort
    if ( empty( $attachments ) )
        return '';

    // RSS/Atom
    if ( is_feed() ) 
    {
        $output = "\n";
        foreach ( $attachments as $att_id => $attachment )
            $output .= wp_get_attachment_link( $att_id, $size, true )."\n";

        return $output;
    }

    $itemtag = tag_escape( $itemtag );
    $captiontag = tag_escape( $captiontag );
    $columns = intval( $columns );
    $itemwidth = $columns > 0 ? floor( 100/$columns ) : 100;
    $float = is_rtl() ? 'right' : 'left';

    $selector = 'gallery-'.$instance;

    $gallery_div = '';

    $size_class = sanitize_html_class( $size );

    $gallery_div = '<div id="'.$selector.'" class="gallery galleryid-'.$id.' gallery-columns-'.$columns.' gallery-size-'.$size_class.'">';

    $i = 0;
    foreach ( $attachments as $id => $attachment ) 
    {
        $class = $columns > 0 && ++$i % $columns == 0 ? ' last' : ''; // ADDED for the single item/image

        $link = isset( $attr['link'] ) && 'file' == $attr['link'] ? wp_get_attachment_link( $id, $size, false, false ) : wp_get_attachment_link( $id, $size, true, false );

        $output .= '<'.$itemtag.' class="gallery-item '.$class.'">'; // ADDED class
        $output .= '<'.$icontag.' class="gallery-icon">'.$link.'</'.$icontag.'>';
        if ( $captiontag && trim($attachment->post_excerpt) )
        {
            $output .= '<'.$captiontag.' class="wp-caption-text gallery-caption">';
            $output .= wptexturize( $attachment->post_excerpt );
            $output .= '</'.$captiontag.'>';
        }
        $output .= '</'.$itemtag.'>';
    }
    $output .= ''."\n";

    return $output;
}
add_filter( 'post_gallery', 'modified_post_gallery', 10, 2 );
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted
<?php
function modified_post_gallery( $blank = NULL, $attr ) 
{
    echo '<pre>'; 
            print_r( $attr ); 
    echo '</pre>';
}
add_filter( 'post_gallery', 'modified_post_gallery', 10, 2);
?>

In media.php it shows the filter like this:

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

Its passing two variables not one and $attr is on the second variable.

share|improve this answer
    
Another copy/paste error ... my callback function is added exactly with ,10, 2. Sorry, I edit my Q. –  kaiser May 18 '11 at 18:51
    
I think this is the right answer. Look at the parameters you pass to the modified_post_gallery function. Define it with a null first parameter like in the code here, and it should work. –  goldenapples May 18 '11 at 19:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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