I have a set of custom theme settings in my functions.php file.
In order to arrange some things I want to load jquery ONLY to my functions.php file.
What I read in wordpress is this:
If it was needed within the admin, you could use the admin_enqueue_scripts action instead, however this enqueues it on ALL admin pages, which often leads to plugin/core conflicts, ultimately breaking the WordPress admin experience. Instead, You should only load it on the individual pages you need it, See the Load scripts only on Plugin pages section for an example of that.
This is the wordpress recommended script for enabling jquery only on plugin pages.
Can I adapt this script to enable my script just for the theme options page?
<?php
add_action( 'admin_init', 'my_plugin_admin_init' );
add_action( 'admin_menu', 'my_plugin_admin_menu' );
function my_plugin_admin_init() {
/* Register our script. */
wp_register_script( 'my-plugin-script', plugins_url('/script.js', __FILE__) );
}
function my_plugin_admin_menu() {
/* Register our plugin page */
$page = add_submenu_page( 'edit.php', // The parent page of this menu
__( 'My Plugin', 'myPlugin' ), // The Menu Title
__( 'My Plugin', 'myPlugin' ), // The Page title
'manage_options', // The capability required for access to this item
'my_plugin-options', // the slug to use for the page in the URL
'my_plugin_manage_menu' // The function to call to render the page
);
/* Using registered $page handle to hook script load */
add_action('admin_print_styles-' . $page, 'my_plugin_admin_styles');
}
function my_plugin_admin_styles() {
/*
* It will be called only on your plugin admin page, enqueue our script here
*/
wp_enqueue_script( 'my-plugin-script' );
}
function my_plugin_manage_menu() {
/* Output our admin page */
}
?>
Thank you!
add_options_page
returns the same value asadd_submenu_page
and can be used in exactly the same way as the code you posted.This will load your scripts only on that page.
You can also request the global
$pagenow
variable if you’d rather do it that way.