Enqueue script for development purposes only?

admin2025-06-05  2

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 *.test domain on my local server. I use if( false !== strpos( $_SERVER['SERVER_NAME'], '.test' ) ) { /* dev functions here */}. – Max Yudin Commented Dec 16, 2018 at 13:10
Add a comment  | 

2 Answers 2

Reset to default 0

It'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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749094439a316315.html

最新回复(0)