• Resolved alfredbchristensen

    (@alfredbchristensen)


    I have a number of custom shortcodes that end up calling functions in my mu-plugin file. I have noticed that my shortcodes are called twice during an http transaction: first via apply_filters to the content and then via apply_filters to the excerpt. This happens with a specific vendor plugin active; if this plugin is deactivated, my shortcodes are invoked once only.
    My basic question is: should my shortcodes be written in such a way that they can handle being invoked more than once within a given http transaction, or should I pursue the issue with the developers of the plugin that causes this behavior.
    As long as a shortcode only returns html, this is not big issue – assuming the returned html is only sent to the browser once. However, if my shortcode does other processing, such as updating a database or sending an email, these actions happen twice unless my code detects it is being invoked a second time and refrain from doing anything that second time.
    Any insight is very much welcome.
    Thanks
    Alfred

Viewing 4 replies - 1 through 4 (of 4 total)
  • Since shortcodes have no control over where they are put, they should not have side effects, but if they do, they should include any logic that is needed to ensure that any side effects happen in the correct conditions.

    Thread Starter alfredbchristensen

    (@alfredbchristensen)

    Thanks Joy. That is what I was beginning to lean towards to. Thank you for confirming. Have you ever seen any design/programming recommendations on how to detect and handle multiple invocations. So far I just keep a global php variable I update during the first invocation and test on subsequent invocations. It seems a little rudimentary, but it does the job for now.

    You can use a static variable, but if you want it just once, you can use a define since they are constants and can only be defined once.

    Thread Starter alfredbchristensen

    (@alfredbchristensen)

    Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Shortcode invoked multiple times’ is closed to new replies.