Support » Developing with WordPress » Shortcode fires when page loads in editor (plugin-dev)

  • I am a new plugin developer. My plugin uses shortcodes. I have noticed that when I go into the WordPress editor to edit a page that contains a shortcode for my plugin, the plugin fires. I only want my plugin to actually do work when the page is being rendered for display and not editing.

    It seems that the plugin is fired other times, too. Sometimes when I have the code in debug mode (in VSCode, using xdebug) and I go over to MySQL admin, the plugin seems to be fired. I don’t understand this at all, but anyway I thought I would start with getting the first question first.

    I am using 5.9 with the “Neve” theme, but this happens with other themes as well. Any help would be appreciated

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    Your main plugin file is loaded on every request. Any explicit statements (those outside of function and class declarations) execute. You can conditionally execute code and exclude back end execution with something like if ( ! admin()):. But the main plugin file loads very early. it’s risky to attempt to call other WP functions at this point since WP is not fully initialized and many functions are unavailable.

    By adding callbacks to filter and action hooks, you defer execution until that hook fires, where WP has already been fully initialized. Some hooks only fire for front or back end requests. For those that fire for either, that’s where it’s appropriate to exclude admin requests.

    Thread Starter Jim Briggs


    Thanks for the explanation. Here’s some more specifics from my case.

    My plugin has code like this

    function myplugin_main($atts=[], $content = null)
    // does stuff

    When I go to edit a page that has the shortcode [MYPLUGIN], the function myplugin_main is fired. When I edit a page that does not have the shortcode, it is not fired. All of this makes a kind of sense, because it seems that WordPress can’t or doesn’t differentiate cases in which the shortcode exists as a shortcode and cases in which the shortcode is meant to function as a shortcode (that is, as a function call).

    What I would like to do is to not have the myplugin_main function fire if the page is just being edited. Or, at least, have myplugin_main exit early if it’s not going to be needed, but I don’t know how to programmatically tell whether the shortcode is going to be interpreted or whether it’s going to be presented merely as the shortcode itself.

    Thanks for the help.

    Moderator bcworkz


    Not only fires in the editor, it apparently fires 3 times in my testing. Not sure why it fires at all since the shortcode return is not displayed, but it surely does.

    Despite plugin code being loaded early, the shortcode callback executes much later, so you can use is_admin() to bail out of the callback early.

    function myplugin_main($atts=[], $content = null)
       if ( is_admin()) return '';
       // does stuff


    Hi, sorry for the intrusion.

    I load my shortcode only on public page using if !admin.
    But, if I have an error inside a shortcode (I coded an error to check), when I update a page, the error “Publishing page” occurs.
    Viceversa, when I delete the coded error line, everything turns to works perfectly

    So, how !admin function works?
    Why my shortcode are checked when I update the page?

    Thank you very much

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.