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.

When I insert a gallery to a blog post using the [gallery] shortcode or simply the insert media button I get a default structured gallery in my blog through the_content(); However the gallery comes with a lot of inline CSS and other stuff I want to remove, how can I do it?

share|improve this question
    
Why was the answer erased??? it was the most helpful I got... –  ilyo Jul 11 '12 at 8:36
    
I deleted my answer because you specifically stated in the comments that you were not talking about the [gallery] shortcode. My answer was on how to filter the shortcode and had nothing to do with a gallery format post. –  Chris_O Jul 11 '12 at 10:06
    
@Chris_O luckily I copy-pasted your code before you removed it and after some modifications it is exactly what I needed. maybe the confusion was since I never used [gallery] but just the insert gallery button –  ilyo Jul 11 '12 at 11:45
    
Anyway, if you want to republish your answer it'll probably be useful, if not i'll just delete this question.. –  ilyo Jul 11 '12 at 11:46
    
Well then it does apply to your question. Could you also edit your question so others will understand that you were referring to the gallery shortcode. –  Chris_O Jul 11 '12 at 17:45

1 Answer 1

up vote 6 down vote accepted

You can filter the default gallery shortcode. Here is something I've used in the past.

add_filter( 'post_gallery', 'wpse_gallery', 10, 2 );

function wpse_gallery() {
    global $post;

    /* Orderby */
    if ( isset( $attr['orderby'] ) ) :
        $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
        if ( !$attr['orderby'] )
            unset( $attr['orderby'] );
    endif;

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

    /*
    * Make sure $id is an integer
    */
    $id = intval( $id );

    /*
    * Get image attachments
    * If none, return
    */
    $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
    if ( empty( $attachments ) )
        return '';

    /*
    * If is feed, leave the default WP settings
    * We're only worried about on-site presentation
    */
    if ( is_feed() ) {
        $output = "\n";
        foreach ( $attachments as $id => $attachment )
            $output .= wp_get_attachment_link( $id, $size, true ) . "\n";
        return $output;
    }

    $i = 0;

    /*
    * Remove the style output in the middle of the freakin' page.
    * This needs to be added to the header.
    * The width applied through CSS but limits it a bit.
    */

    /*
    * Open the gallery <div>
    */
    $output .= '<div id="gallery-'.$id.'" class="content gallery gallery-'.$id.'">'."\n";
    $output .= '<div id="thumbs" class="navigation">'."\n";
    $output .= '<ul class="thumbs noscript">'."\n";
    /*
    * Loop through each attachment
    */
    foreach ( $attachments as $id => $attachment ) :

        /*
        * Get the caption and title
        */
        $caption = esc_html( $attachment->post_excerpt, 1 );
        $title = esc_html( $attachment->post_title, 1 );
        $link = wp_get_attachment_image_src( $id, 'large' );
        $img = wp_get_attachment_image_src( $id, $size );

        /*
        * Open each gallery item
        */
        $output .= "\n\t\t\t\t\t<li class='gallery-item'>";
            $output .= '<a class="thumb" href="' .  wp_get_attachment_url( $id ) . '" title="' . $title . '">';
                $output .= '<img src="' . $img[0] . '" alt="' . $title . '" title="' . $title . '" />';
            $output .= '</a>';

        /*
        * If image caption is set
        */
        if ( $caption ) :
            $output .= "\n\t\t\t\t\t\t<div class='caption'>";
                $output .= $caption;
            $output .= "</div>";
        endif;

        /*
        * Close individual gallery item
        */
        $output .= "\n\t\t\t\t\t</li>";

    endforeach;

    /*
    * Close gallery and return it
    */

        $output .= '</ul><!--.thumbs-->'."\n";
        $output .= '</div><!--#thumbs-->'."\n";
    /*
    * Return out very nice, valid XHTML gallery.
    */
    return $output;

}
share|improve this answer

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.