I am building a wordpress theme with a theme dashboard with options, but as I read somewhere, the enqueue script code I am using just enqueues it in the backend but it loads in every page in the backend. So I am having some js conflicts…
I have this code:
add_menu_page($themename, $themename, 'administrator', basename(__FILE__), 'ikos_admin');
add_submenu_page(basename(__FILE__), $themename . ' Options', 'Theme Options', 'administrator', basename(__FILE__),'ikos_admin'); // Default
}
function ikos_add_init() {
$file_dir=get_bloginfo('template_directory');
wp_enqueue_style("ikosCss", $file_dir."/functions/theme-options.css", false, "1.0", "all");
wp_enqueue_script("ikosScript", $file_dir."/functions/theme-options.js");
wp_enqueue_script("ikospickerScript", $file_dir."/functions/color-picker.js", array( 'farbtastic', 'jquery' ) );
wp_enqueue_style( 'farbtastic' );
wp_enqueue_style( 'farbtastic' );
wp_register_script('my-upload', $file_dir."/functions/upload.js");
wp_enqueue_script('my-upload');
if(function_exists( 'wp_enqueue_media' )){
wp_enqueue_media();
}else{
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}
}
So, how do I enqueue those scripts in that specific admin page only???
Thanks 😀
EDIT
YEAH NOW IT IS WORKING!
New code using the hook, then I added the var_dump( $hook_suffix ); exactly before the abort/return statement as @kaiser told me in the comments and it printed this: **string(0) “” string(27) “toplevel_page_theme-options” **.
So I just added the “toplevel_page_theme-options” in the ikos_add_init($hook_suffix) and it works!
$hook_suffix = add_menu_page($themename, $themename, 'administrator', basename(__FILE__), 'ikos_admin');
add_submenu_page(basename(__FILE__), $themename . ' Options', 'Theme Options', 'administrator', basename(__FILE__),'ikos_admin'); // Default
}
add_action( 'admin_enqueue_scripts', 'ikos_add_init' );
function ikos_add_init($hook_suffix) {
if ( 'toplevel_page_theme-options' !== $hook_suffix )
return;
$file_dir=get_template_directory_uri();
wp_enqueue_style("ikosCss", $file_dir."/functions/theme-options.css", false, "1.0", "all");
wp_enqueue_script("ikosScript", $file_dir."/functions/theme-options.js");
wp_enqueue_script("ikospickerScript", $file_dir."/functions/color-picker.js", array( 'farbtastic', 'jquery' ) );
wp_enqueue_style( 'farbtastic' );
wp_enqueue_style( 'farbtastic' );
wp_register_script('my-upload', $file_dir."/functions/upload.js");
wp_enqueue_script('my-upload');
if(function_exists( 'wp_enqueue_media' )){
wp_enqueue_media();
}else{
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}
}
Inside
admin-header.php
, there’s the following set of hooks:The one to always use it
admin_enqueue_scripts
, both for stylesheet and scripts. More info in this answer.It has one additional argument, the
$hook_suffix
. This argument is exactly the same as the return value that you get fromadd_submenu_page()
and the related (shorthand) functions.Example
Note: The following example assumes that you already registered the script previously, so it’s available with the handle/identifier of
your_handle
.Additional Notes
As I see that you’re identifying the Themes root directory using
get_option('template_directory')
, I have to leave a short additional note as this isn’t how it’s done nowadays, as this info then comes from the DB, which want to avoid. It as well will ignore the default filters that are in place for that:get_template_directory()
– Path to the parent theme root dirget_stylesheet_directory()
– Path to the child theme root dirget_template_directory_uri()
– URL to the parent theme root dirget_stylesheet_directory_uri()
– URL to the child theme root dirplugin_dir_path( __FILE___ )
– Root of the current file, no matter if in child/parent theme or a plugin. Trailing slashed result.Then there’s
wp_get_theme()
, which calls an instance ofWP_Theme
for the currently active theme.If you got a child theme, you can as well simply call
$current_theme->parent()
to access all the otherWP_Theme
methods on the parent themes instance. For e.g.This then also allows to access header information from the theme. For e.g. the
Name
,DomainPath
, etc.Look at the URL of the page you want to enqueue the js into. It might be sth like
Then just use that like