• It appears that FSE theme breaks inline scripts.

    I noticed this in 2022 but tested with another (WABI) with the same result.

    I coded a simple POC

    – register a script file
    – add a shortcode that
    — enqueues the script file
    — adds inline script

    When the shortcode is displayed in non FSE themes it works as expected
    but in FSE themes the inline script does not run

    I THINK I doing this the right way, but open to the wisdom of others.

    POC as a plugin here https://github.com/alanef/inline-script-test

Viewing 11 replies - 1 through 11 (of 11 total)
  • I tested the POC plugin with the Archeo (block) theme and the latest WP 6.0 dev branch, and it is working: https://cldup.com/54uNwN9T7X.jpg

    Are you running against 5.9? I’ll do some digging to see if there was an issue that has since been corrected.

    Thread Starter Alan Fuller

    (@alanfuller)

    6.0-RC3

    I’ll try Archeo

    p.s. I have a solution, bit of a hack by experimentation

    Thread Starter Alan Fuller

    (@alanfuller)

    Just FYI it doesn’t work for me on Archeo

    Only the script file not the inline

    2022-05-19-21-02<br />

    Thread Starter Alan Fuller

    (@alanfuller)

    The hack solution is to register the script inside the shortcode function. Not nice but works with FSE and non FSE themes.

    I re-opened that Trac as the solution is also a hack adding script into the body of markup via the shortcode output, as it doesn’t solve the issue I came across which was wp_add_inline_script ‘before’

    Ah, I see what I missed earlier too. I thought the console.log() call was just for the inline script and not both. No, it’s not working for me either.

    Reopening that ticket was the best option. I don’t know if the advice given there was ideal because this is likely to break many plugins.

    Thread Starter Alan Fuller

    (@alanfuller)

    As it happened, although I was ‘before’-ing my inline script on testing it didn’t matter ( as the data isn’t used until the dom is loaded ) so for the moment I’m now echoing the script.

    I was also worried that localize_script would fail inside the shortcode – but actually that works fine – which is inconsistent to say the least.

    – register a script file
    – add a shortcode that
    — enqueues the script file
    — adds inline script

    @alanfuller – does this not add the inline script every time the shortcode is added to the page. For example: if there are 3 times the shortcode is used on the page?

    Thread Starter Alan Fuller

    (@alanfuller)

    No enqueue script only enqueues scripts that have not been enqueued.

    Basically it looks up in a global to see if it needs to be added and only adds if it is not already listed.

    You can easily test by adding the same enqueue several times.

    — adds inline script

    I mean the inline script.

    Thread Starter Alan Fuller

    (@alanfuller)

    Yes, of course, that is the point of using inline script, if you didn’t need to add it with each shortcode your would just enqueue a js file with the contents.

    So just to explain further if your shortcode has arguments (atts) and you need to pass the values down to javascript you need a mechanism to bridge php to javascript. That is what add_inline_script is designed for.

    It works perfectly, but is broken by FSE themes.

    • This reply was modified 2 years, 9 months ago by Alan Fuller.
Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘FSE Themes seems to ‘break’ wp_inline_script()’ is closed to new replies.