Support » Fixing WordPress » Prevent gutenberg from rendering shortcodes when editing

  • sessioncookiemonster


    I have a plugin, which is a shortcoder with an analytics module that increments its required fields when a relevant shortcode is being rendered.

    When using Gutenberg, those counters are incremented every time the “edit post” page is opened if it contains the shortcodes. (I think that I’ve seen it increment them even if there are none, but I was unable to replicate it)

    Currently I’m turning off the analytics module if the request is to the REST API, like
    if(defined('REST_REQUEST') && REST_REQUEST ) $this->isOn = false;
    , because the increments used to happen on save as well.

    What would be a good way to catch all of the requests that are made to pages with gutenberg(“add new post/page” page, save post action etc) to prevent the analytics from firing when it shouldn’t?

    • This topic was modified 1 year, 4 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not a Developing with WordPress topic
Viewing 3 replies - 1 through 3 (of 3 total)
  • Honestly, this sounds like a misuse of shortcodes. I would strongly suggest implementing this analytics behaviour a different way, rather than messing with the editor behaviour like this.

    Thread Starter sessioncookiemonster


    Incrementing the analytics is part of the routine that renders the shortcode content, I see nothing wrong with that, since the shortcodes aren’t supposed to be expanded in the editor view at all!
    The real question here is why does the gutenberg editor render the shortcodes in the first place? And how does it do it? I mean, the shortcodes appear unexpanded in the editor view, so surely it is some kind of a bug, right?
    Keep in mind that we’re talking about random shortcodes appearing in the post body(in html blocks etc)

    Is there any update on this, or a workaround?

    I have the same issue. I’d prefer the shortcodes to *not* render on the edit page, but it definitely seems like a bug to both show the shortcodes as text *and* to render them. They end up stacking on top of each other in a clutter.

    Is there a way my code can detect when the page is being edited (from within the shortcode rendering function) so it can output some stub html instead of the full rendered html?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Prevent gutenberg from rendering shortcodes when editing’ is closed to new replies.