How do I change the gallery that is inserted in the post?

When I insert a gallery to a blog post using the 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?

Related posts

Leave a Reply

1 comment

  1. 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 .= "nttttt<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 .= "ntttttt<div class='caption'>";
                    $output .= $caption;
                $output .= "</div>";
            endif;
    
            /*
            * Close individual gallery item
            */
            $output .= "nttttt</li>";
    
        endforeach;
    
        /*
        * Close gallery and return it
        */
    
            $output .= '</ul><!--.thumbs-->'."n";
            $output .= '</div><!--#thumbs-->'."n";
        /*
        * Return out very nice, valid XHTML gallery.
        */
        return $output;
    
    }