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');
}
}