Support » Themes and Templates » Extracting a post's gallery shortcode (if it contains one)?

  • Possible? I have a unique situation where I need to be able to tell if a post contains a gallery shortcode and if it does, extract it and use it elsewhere. I know I can tell if a post contains the shortcode using this:

    if ( gallery_shortcode($post->ID) ){
    //do stuff
    }

    but I’m not sure what my next step would be. I’d like to do something like this:

    if ( gallery_shortcode($post->ID) ){
    	$gallery = extract_gallery() //(function that I'm hoping exists);
    	do_shortcode($gallery);
    	echo "<div id='wrapper-div'">;
    	echo strip_shortcodes($post->post_content);
    	echo "</div>";
    }

    I’ll keep digging around, but I figured I’d post here first in the meantime… Is there a built-in WP function that would allow me to do this or am I going to have to do a preg_match or something?

    Any help provided would be much appreciated

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Not entirely sure if that conditional will work.

    What are you actually trying to do and where are you trying to relocate the gallery?

    It appears to. I can’t remember where I found it, but it’s definitely working in my local test environment in the following context:

    if ( !gallery_shortcode($post->ID) ){
    	echo '<h1>' . get_the_title() . '</h1>';
    	the_content();
    }

    Essentially I’m trying to give the posts authors the ability to add a gallery to the post, but let me chose where to display it regardless of where they decide to put it. I’m aware that I could just extract all the images attached to the post and use them that way, but I’d like for the authors to be able to use the built-in gallery manager to control order, captions, etc. Make sense?

    Actually, this turns out to be reasonably easy using get_shortcode_regex() (assuming of course that your gallery will be the first thing in the post). Here’s what I ended up with:

    if ( gallery_shortcode($post->ID) ){
    	$pattern = get_shortcode_regex();
    	$matches = array();
    	preg_match("/$pattern/s", get_the_content(), $matches); //just finds the first one
    	echo do_shortcode($matches[0]);
    }

    Now, my problem becomes that I don’t know how to display the content without showing the gallery… It seems like stripping shortcodes from your content is kind of an all-or-nothing proposition…

    I want to do the exact same.

    Allow the galleries to be controlled via the CMS by clients, but have the gallery display exactly where I want in my custom template.

    I could only think of similar solutions, stripping the shortcode somehow, then reinserting it elsewhere in the template outside of the content…

    I’ll be interested in seeing if there is a solution.
    I’ll try your method and see where I get!

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    You can try using: preg_replace()

    Works very well. Thanks @b1gj4k3!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Extracting a post's gallery shortcode (if it contains one)?’ is closed to new replies.