Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
This doesn't make much sense, functions.php is not a template file, do you mean only load the script on the frontend, and not the backend? –  Tom J Nowell Jul 3 '12 at 20:10
    
ah nevermind I've edited your questions title to make more sense –  Tom J Nowell Jul 3 '12 at 20:11
    
Yes, it makes much more sense... functions.php is a page from my backend located in my theme folder. I need to load jquery to it in order to do some styling –  webmasters Jul 3 '12 at 20:13
    
jQuery is already loaded on all of the WordPress backend pages, you don't need to load it again. –  EAMann Jul 3 '12 at 20:42
    
in functions.php without loading jquery the script does not work –  webmasters Jul 3 '12 at 20:47

1 Answer 1

up vote 0 down vote accepted

add_options_page returns the same value as add_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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.