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.
6.0-RC3
I’ll try Archeo
p.s. I have a solution, bit of a hack by experimentation
Just FYI it doesn’t work for me on Archeo
Only the script file not the inline
<br />
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.
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?
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.
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.