2

I have a page which only contains this:

[memberdirectory] [/memberdirectory]

It's purpose is to list names and addressed of members. It loads and runs the associated function without any problem. My problem is that when I went to change the page the shortcode is executed when the page is loaded for editing. In other words, when I pick the page on the dashboard to edit. This happening is not a killer, just an inconvenience. I must not understand something about shortcodes and when they run. Any help will be appreciated. Thanks,

3
  • Probably something I am not familiar with but would you please say where you get this, is it MembershipWorks? If it's a paid product they have support? To me, I've never seen a "closing tag" on a shortcode, meaning the second part [/memberdirectory] looks weird to me. What happens if you remove the second shortcode? (NVM I see in codex.wordpress.org/Shortcode_API there are shortcodes with "closing tag" / characters)
    – JimLohse
    Commented Jun 29, 2019 at 16:17
  • and still just guessing but I suspect this may be normal for shortcodes in the Visual editor, whereas you see the actual shortcode in the Text editor.
    – JimLohse
    Commented Jun 29, 2019 at 16:17
  • This is a standard Wordpress page. Nothing fancy. I removed the closing tag and got the same result. I did notice that in the loading, the Wordpress form to modify page overlays the output from the shortcode handler. Thanks Jim,
    – Bud
    Commented Jun 29, 2019 at 20:45

1 Answer 1

2

I've faced a similiar situation on couple of occasions. In my case the shortcode getting rendered on the text editor was caused by the shortcode echoing instead of returning its content.

As noted on add_shortcode Codex entry,

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

My suggestion is that you double-check the output of the shortcode so that it returns stuff instead of echoing it.

If it's echoing the output and you don't feel like rewriting everything, you could wrap the echos inside output buffer, like so.

function some_shortcode( $atts, $content = null ) {

  // start output buffer, which will hold all the following echoed html
  ob_start();

  // old code echoing html

  // return html content from buffer and clear buffer
  return ob_get_clean();

}

If this doesn't help, then please share your code so the community can have a better look at it.

2
  • In my code, I'd used ob_get_contents() and ob_end_clean(); but the ob_start() got lost. As soon as I added ob_start the problem went away.
    – Bud
    Commented Jun 30, 2019 at 14:46
  • +1 echoing code in a shortcode will break your page editor if the page has the shortcode on.
    – AutoBaker
    Commented Oct 18, 2022 at 16:05

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.