admin管理员组文章数量:1416631
I'm trying not to be a lazy dev. Thus I want to only include my js files where they are needed.
As the js is needed everywhere where my shortcode is used I tried to do what was answered here.
i.e.:
js
function do_cool_stuff(){
//do the most awesomest of stuff
//..ok mostly changes textvalues dynamically
}
php
function enqueueAllMyStuff(){
\wp_enqueue_script('my_stuff',path_to_my_stuff);
\wp_localize_script('my_stuff','jabba_the_slug',['url'=>\admin_url('admin-ajax.php')];
//...
}
\add_shortcode('goodName',__NAMESPACE__.'\\my_shortcode')
function my_shortcode(){
enqueueAllMyStuff();
return '<script>do_cool_stuff()</script>';
}
But that did not work. As "do_cool_stuff is not defined". Tried to do
return '<img src="'.admin_url().'/images/loading.gif" onload="do_cool_stuff()"/>';
Which works nicely from time to time. Implying its a race condition.
function my_shortcode(){
\add_action('wp_enqueue_scripts',__NAMESPACE__.'\\enqueueAllMYStuff');
return '<script>do_cool_stuff()</script>';
}
also does not work, as the headers are already send.
so... There ought to be a better way to do this.
I'm trying not to be a lazy dev. Thus I want to only include my js files where they are needed.
As the js is needed everywhere where my shortcode is used I tried to do what was answered here.
i.e.:
js
function do_cool_stuff(){
//do the most awesomest of stuff
//..ok mostly changes textvalues dynamically
}
php
function enqueueAllMyStuff(){
\wp_enqueue_script('my_stuff',path_to_my_stuff);
\wp_localize_script('my_stuff','jabba_the_slug',['url'=>\admin_url('admin-ajax.php')];
//...
}
\add_shortcode('goodName',__NAMESPACE__.'\\my_shortcode')
function my_shortcode(){
enqueueAllMyStuff();
return '<script>do_cool_stuff()</script>';
}
But that did not work. As "do_cool_stuff is not defined". Tried to do
return '<img src="'.admin_url().'/images/loading.gif" onload="do_cool_stuff()"/>';
Which works nicely from time to time. Implying its a race condition.
function my_shortcode(){
\add_action('wp_enqueue_scripts',__NAMESPACE__.'\\enqueueAllMYStuff');
return '<script>do_cool_stuff()</script>';
}
also does not work, as the headers are already send.
so... There ought to be a better way to do this.
Share Improve this question asked Aug 8, 2019 at 15:45 TheoMTheoM 32 bronze badges1 Answer
Reset to default 1If you run wp_enqueue_script()
inside a shortcode, which is essentially what you're doing by calling enqueueAllMyStuff()
, the script is going to be queued up to be loaded in the footer. So if your shortcode runs a script as soon as it's printed, the JavaScript file that it depends on won't be available yet, and will cause this error.
The simplest solution is to update your script to wait for the document to have loaded. With jQuery you'd use jQuery( document ).ready()
, but in vanilla JS you can use:
<script>
window.addEventListener( 'DOMContentLoaded', function() {
do_cool_stuff();
} );
</script>
本文标签: plugin developmentHow do I get rid of my inclusion racecondition on wpenqueuescript
版权声明:本文标题:plugin development - How do I get rid of my inclusion race-condition on wp_enqueue_script 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745256518a2650136.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论