Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Help with unhook or disable WordPress galleries native behavior.

Help with unhook or disable WordPress galleries native behavior.

Hi Treehouse, I'm trying to stop the WordPress default behavior, that loads galleries as part of the_content. Becasue I want to call the get_post_gallery myself so that it become a separate element in the markup.

<?php /* The loop */
while ( have_posts() ) : the_post();
    if ( get_post_gallery() ) :
        echo get_post_gallery();
    endif; 
endwhile; 
?>

And make it possible for me to add custom fields in-between the the_content and the gallery.

I've been looking at the media.php in the wp-includes folder for clues. I'm not sure how to go about this. From what I can see it's a filter hook... I've been trying to Google how to unhook the gallery from firing by default. But no luck.

Can anyone help me achieve this? Or know how to pull it off?

Thanks. I hope my question makes sense.

Doru Marginean
Doru Marginean
2,290 Points

Hi Christian Steen Jørgensen,

You can try the following code into functions.php

<?php
/**
 * Remove gallery shortcode from content
 */

function remove_gallery_shortcode( $content ) {

    global $post, $shortcode_tags;

    // apply the filter only to posts
    if( $post->post_type == 'post' ) {

        if ( false === strpos( $content, '[' ) ) {
                return $content;
        }
        if (empty($shortcode_tags) || !is_array($shortcode_tags))
                return $content;
        // Find all registered tag names in $content.
        preg_match_all( '@\[([^<>&/\[\]\x00-\x20=]++)@', $content, $matches );
        $tagnames = array_intersect( array_keys( $shortcode_tags ), $matches[1] );
        if ( empty( $tagnames ) ) {
                return $content;
        }

        $gallery_tagname = array();
        foreach( $tagnames as $tag_id => $tag_name ) {
            if( $tag_name == 'gallery' ) {
                $gallery_tagname[1] = $tag_name;
            }
        }

        if ( empty( $gallery_tagname ) ) {
            return $content;
        }

        $content = do_shortcodes_in_html_tags( $content, true, $gallery_tagname );
        $pattern = get_shortcode_regex( $gallery_tagname );
        $content = preg_replace_callback( "/$pattern/", 'strip_shortcode_tag', $content );
        // Always restore square braces so we don't break things like <!--[if IE ]>
        $content = unescape_invalid_shortcodes( $content );
    }

    return $content;

}
add_filter( 'the_content', 'remove_gallery_shortcode' );
?>

And your single.php would look something like this inside the loop:

<?php
    // default post content without gallery shortcode
    the_content();

    // add custom content here
    echo '<p>Lorem ipsum..</p>';

    if ( get_post_gallery() ) :
        // get all the galleries
        $galleries = get_post_galleries( $post );

       // choose whether to show the first gallery or all the galleries

    // 1. show only the first gallery
        echo get_post_gallery(); 

        // 2. show all the galleries
        foreach( $galleries as $gallery ) :
        echo $gallery;
    endforeach;
    endif;
?>

Hey Doru Marginean, wow thanks...

I haven't had the opportunity yet to test this out, but I will soon. Hopefully I can let you know that it works. Thanks again.