WordPress.org

Ready to get started?Download WordPress

Forums

Extracting a post's gallery shortcode (if it contains one)? (7 posts)

  1. B1gJ4k3
    Member
    Posted 1 year ago #

    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

  2. Not entirely sure if that conditional will work.

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

  3. B1gJ4k3
    Member
    Posted 1 year ago #

    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?

  4. B1gJ4k3
    Member
    Posted 1 year ago #

    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...

  5. LingoSam
    Member
    Posted 1 year ago #

    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!

  6. You can try using: preg_replace()

  7. Carlo Rizzante
    Member
    Posted 1 year ago #

    Works very well. Thanks @B1gJ4k3!

Topic Closed

This topic has been closed to new replies.

About this Topic