Preventing plugin output corrupting page update response

[ad_1]

I have a self-written plugin to render output on various pages.

The problem is that the post response for the update process is running the shortcode when saving the page:

For example, the page contains a shortcode block with `[abcde]`

My plugin handles shortcode_abcde and outputs:
“`<h1>ABCDE</h1>“`

This works fine on the page. But then it also happens on save so the JSON response contains:

<h1>ABCDE</h1>
{“id”:11364,”date”:”2022-12-27T12:22:36″,”date_gmt”:”2022-12-27T12:22:36″, … “type”: “page” }

which is obviously invalid JSON

What can I do in my plug-in to detect I’m in the editor and not run any code?

[ad_2]
1 Comment
  1. Oh man, I’m an idiot
    > Note that the function called by the shortcode should never produce an 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 unexpected side effects from the call since you cannot control when and where they are called from.

    Now I have to rewrite a tonne of code 🙁

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer