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