1

I need to execute a short code on thousands of posts which don't already contain the short code as i've added some manually.

I'm using another function to add the opening short code after the 2nd paragraph which works.

I've written this code which outputs the closing short code after the content but it does't execute.

add_filter( 'the_content', 'closing_shortcode' );
function closing_shortcode( $content ) {

        if( !has_shortcode( $content, 'members') )
        return $content;

        $close_shortcode = do_shortcode('[/member]');

        return $content . $close_shortcode;
}       

Maybe i need to use echo do_shortcode.

8
  • I think the problem is that you're calling do_shortcode() on only half the sortcode, the ending half which won't translate well. What happens whenever you remove do_shortcode() and just append the closing shortcode string?
    – Howdy_McGee
    Apr 16, 2015 at 15:48
  • @Howdy_McGee Still outputs the shortcode but doesn't execute. Apr 16, 2015 at 16:05
  • I created a simple shortcode which outputs container <div> and was able to use the above successfully ( both with do_shortcode() and with just appending the ending shortcode tag ). It could be that there's a bunch of junk content between your opening shortcode and ending shortcode ( maybe empty paragraph tags ) that's keeping it from operating properly. View Source / Dev Tools to see if this is the case. If your shortcode relies on JS check for console errors.
    – Howdy_McGee
    Apr 16, 2015 at 16:19
  • 1
    Can't you use a single function to add the opening and closing shortcode at same time?
    – gmazzap
    Apr 16, 2015 at 20:25
  • 3
    Stay cool, calm and collected - people are just trying to help you, right? Apr 18, 2015 at 11:13

1 Answer 1

0

Maybe I misunderstand what you are doing, and I apologize if that is the case, but...

  1. It looks like you are returning content if the post DOES NOT already have one, which looks backward to me based on my understanding of your problem.
  2. It looks like you are trying to execute a broken (incomplete) shortcode.

I misunderstood the original question but based on comments below, you need to run do_shortcode() on the whole content block, not just on the closing section.

I think your code should look like:

add_filter( 'the_content', 'closing_shortcode' );
function closing_shortcode( $content ) {

        // return if the shortcode has already been added
        // Note the ! was removed
        if( has_shortcode( $content, 'members') )
        return $content;

        // Add the closing sortcode tag
        $close_shortcode = '[/member]';

       // and run do_shortcode() on the whole content block
        return do_shortcode($content . $close_shortcode);
}      

If that doesn't work, it will take some manipulation of the core content and shortcode execution systems and I will have to tackle later, when I have more time.

4
  • [member] is the opening short code which is already added to another function which outputs it after the 2nd paragraph. What i need to do is execute the closing short code [/member] after the content to hide the content after the 2nd paragraph. Apr 16, 2015 at 17:41
  • Oh!!!! I get it. This is much more complicated than I thought.
    – s_ha_dum
    Apr 16, 2015 at 17:49
  • Isn't do_shortcode() hooked to the_content with priority 11, and shouldn't the shortcode just be parsed if added to the content beforehand, or am I missing something? @BradDalton Apr 18, 2015 at 11:26
  • Yes. It doesn't parse for some reason. Apr 21, 2015 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.