I'm trying to change the way a plugin register's its own js file from this:
function gpvd_enqueue_styles_admin() {
wp_enqueue_style(
'gpvb-admin-styles',
plugin_dir_url( __FILE__ ) . 'css/admin.css',
array(),
'',
'all'
);
wp_register_script(
'gpvb-admin-scripts',
plugins_url( 'js/block.js', __FILE__ ),
array( 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-editor' ),
filemtime( plugin_dir_path( __FILE__ ) . 'js/block.js' ),
false
);
wp_enqueue_script( 'gpvb-admin-scripts' );
}
and change the false
to a true
.
So I put this in my functions.php
file:
function sh_child_enqueue_styles_admin() {
wp_enqueue_style(
'gpvb-admin-styles',
plugin_dir_url( __FILE__ ) . 'css/admin.css',
array(),
'',
'all'
);
wp_register_script(
'gpvb-admin-scripts',
plugins_url( 'js/block.js', __FILE__ ),
array( 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-editor' ),
filemtime( plugin_dir_path( __FILE__ ) . 'js/block.js' ),
true
);
wp_enqueue_script( 'gpvb-admin-scripts' );
}
remove_action('admin_enqueue_scripts', 'gpvb_enqueue_styles_admin');
add_action('admin_enqueue_scripts', 'sh_child_enqueue_styles_admin');
However, it can't find the plugin's js file because of __FILE__
. Do I have to hard code the path in there or is there a better way of accomplishing this?
I'm trying to change the way a plugin register's its own js file from this:
function gpvd_enqueue_styles_admin() {
wp_enqueue_style(
'gpvb-admin-styles',
plugin_dir_url( __FILE__ ) . 'css/admin.css',
array(),
'',
'all'
);
wp_register_script(
'gpvb-admin-scripts',
plugins_url( 'js/block.js', __FILE__ ),
array( 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-editor' ),
filemtime( plugin_dir_path( __FILE__ ) . 'js/block.js' ),
false
);
wp_enqueue_script( 'gpvb-admin-scripts' );
}
and change the false
to a true
.
So I put this in my functions.php
file:
function sh_child_enqueue_styles_admin() {
wp_enqueue_style(
'gpvb-admin-styles',
plugin_dir_url( __FILE__ ) . 'css/admin.css',
array(),
'',
'all'
);
wp_register_script(
'gpvb-admin-scripts',
plugins_url( 'js/block.js', __FILE__ ),
array( 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n', 'wp-editor' ),
filemtime( plugin_dir_path( __FILE__ ) . 'js/block.js' ),
true
);
wp_enqueue_script( 'gpvb-admin-scripts' );
}
remove_action('admin_enqueue_scripts', 'gpvb_enqueue_styles_admin');
add_action('admin_enqueue_scripts', 'sh_child_enqueue_styles_admin');
However, it can't find the plugin's js file because of __FILE__
. Do I have to hard code the path in there or is there a better way of accomplishing this?
- 1 So you're trying to change an enqueued script to load in the footer instead of the header? – Tom J Nowell ♦ Commented Jan 9, 2020 at 14:07
- Does this matter, particularly if it's an admin script? I can't imagine it makes much difference to the page load time or could possibly break anything. – Rup Commented Jan 9, 2020 at 14:39
- 1 The way it currently loads conflicts with the customization js of a theme I'm using: wordpress/support/topic/js-conflict-with-shapely-theme – StevieD Commented Jan 9, 2020 at 15:14
1 Answer
Reset to default 1It feels slightly fragile, but you can probably just do:
wp_scripts()->add_data( 'gpvb-admin-scripts', 'group', 1 );
in a low-priority admin_enqueue_scripts hook, since that's what wp_register_script does with the $is_footer flag. However this risks a future version of WordPress changing how this is saved internally (although it's been stable for 5+ years).
Alternatively you could query for the existing entry and use the dependency properties to overwrite the existing dependency entry using wp_register_script again:
$existing_script = wp_scripts()->query( 'gpvb-admin-scripts' );
if ( $existing_script ) {
// Overwrite existing registration
wp_deregister_script( $existing_script->handle );
wp_register_script( $existing_script->handle,
$existing_script->src,
$existing_script->deps,
$existing_script->ver,
true );
}
ditto in a low-priority admin_enqueue_scripts hook.