admin管理员组文章数量:1130349
I'm trying to create a new development workflow and for that I need to enqueue some scripts to be available only when I'm developing. What is the correct way to do this? Should I use WP_DEBUG as a condition or is there a better way?
if (WP_DEBUG === true) {
wp_enqueue_script('script_for_development', 'script.js'...);
}
I'm trying to create a new development workflow and for that I need to enqueue some scripts to be available only when I'm developing. What is the correct way to do this? Should I use WP_DEBUG as a condition or is there a better way?
if (WP_DEBUG === true) {
wp_enqueue_script('script_for_development', 'script.js'...);
}
Share
Improve this question
edited Dec 16, 2018 at 13:01
cjbj
15k16 gold badges42 silver badges89 bronze badges
asked Dec 16, 2018 at 12:32
RunnickRunnick
1,0593 gold badges14 silver badges27 bronze badges
1
|
2 Answers
Reset to default 0It's not immediately clear what the circumstances of your WP install are. WP_DEBUG is a sitewide constant that sits in your wp-config.php, so if you use that as a condition the scripts will be loaded for everybody working in the same install. To toggle it, you must edit wp-config.php, which is fine if that's what you want. It doesn't make a lot of sense, though, because it everybody is allowed to develop you might as well leave the condition out.
If there are other people working in the same install you may want to narrow down your condition, by binding it to login conditions. You could limit loading the scripts to administrators like this:
if (current_user_can('administrator')) { load the script; }
Or you can tie it to individual users like this:
$current_user = wp_get_current_user();
if ($current_user->user_login == 'yitzhaki') { load the script;}
So it depends on the conditions under which you are developing what the smartest approach is. You may also want to remove this code when you distribute your theme.
I think that generally, not wordpress related, you should always be explicit as possible. is there a development mode? state it with a WP_ENV flag that can evaluate to "DEV", "PRODUCTION", "QA" etc.. accordingly, you might even decide to change the value of WP_DEBUG.
I think that the right answer is to create conventions that will make sense to you
I'm trying to create a new development workflow and for that I need to enqueue some scripts to be available only when I'm developing. What is the correct way to do this? Should I use WP_DEBUG as a condition or is there a better way?
if (WP_DEBUG === true) {
wp_enqueue_script('script_for_development', 'script.js'...);
}
I'm trying to create a new development workflow and for that I need to enqueue some scripts to be available only when I'm developing. What is the correct way to do this? Should I use WP_DEBUG as a condition or is there a better way?
if (WP_DEBUG === true) {
wp_enqueue_script('script_for_development', 'script.js'...);
}
Share
Improve this question
edited Dec 16, 2018 at 13:01
cjbj
15k16 gold badges42 silver badges89 bronze badges
asked Dec 16, 2018 at 12:32
RunnickRunnick
1,0593 gold badges14 silver badges27 bronze badges
1
-
1
All my sites under development has my custom
*.testdomain on my local server. I useif( false !== strpos( $_SERVER['SERVER_NAME'], '.test' ) ) { /* dev functions here */}. – Max Yudin Commented Dec 16, 2018 at 13:10
2 Answers
Reset to default 0It's not immediately clear what the circumstances of your WP install are. WP_DEBUG is a sitewide constant that sits in your wp-config.php, so if you use that as a condition the scripts will be loaded for everybody working in the same install. To toggle it, you must edit wp-config.php, which is fine if that's what you want. It doesn't make a lot of sense, though, because it everybody is allowed to develop you might as well leave the condition out.
If there are other people working in the same install you may want to narrow down your condition, by binding it to login conditions. You could limit loading the scripts to administrators like this:
if (current_user_can('administrator')) { load the script; }
Or you can tie it to individual users like this:
$current_user = wp_get_current_user();
if ($current_user->user_login == 'yitzhaki') { load the script;}
So it depends on the conditions under which you are developing what the smartest approach is. You may also want to remove this code when you distribute your theme.
I think that generally, not wordpress related, you should always be explicit as possible. is there a development mode? state it with a WP_ENV flag that can evaluate to "DEV", "PRODUCTION", "QA" etc.. accordingly, you might even decide to change the value of WP_DEBUG.
I think that the right answer is to create conventions that will make sense to you
本文标签: Enqueue script for development purposes only
版权声明:本文标题:Enqueue script for development purposes only? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749094414a2315025.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


*.testdomain on my local server. I useif( false !== strpos( $_SERVER['SERVER_NAME'], '.test' ) ) { /* dev functions here */}. – Max Yudin Commented Dec 16, 2018 at 13:10