WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] WP 3.0 – Problems enqueueing javascript with specific target

[Resolved] WP 3.0 – Problems enqueueing javascript with specific target

  • victheone

    @victheone

    My organization requires that I use WordPress 3.0 to create a new plugin. I’m having trouble getting one of my scripts to load to *only* my Options page (accessed as admin from the dashboard). When I enqueue it without a target page hook, the script successfully loads on every page, but I don’t want that. Here’s a few code snippets:

    In my sidebar loader:
    define( OPTIONS_PAGE, add_menu_page('EMS Forms Options', 'EMS Forms', 'manage_options', PATH.'/EMSF_Options.php') );

    In my main file:
    add_action('admin_print_scripts-'.OPTIONS_PAGE, 'Add_EMSF_Options_Scripts');

    And here’s the function that the main file is assigning the action…

    function Add_EMSF_Options_Scripts(){
    wp_register_script('EMSF_Options, plugins_url('emsforms/js/EMSF_Options.js'), array('jquery','jquery-form'), '1.0');
    wp_enqueue_script('EMSF_Options');
    }

    Any help with this would be greatly appreciated. I suppose I could just let it load the script on every page, but I’d rather not, since it uses jquery’s ajax functionality every time it loads.

    Thanks in advance.

Viewing 4 replies - 1 through 4 (of 4 total)
  • accessed as admin from the dashboard

    Use is_admin() or is_user_logged_in()

    function Add_EMSF_Options_Scripts(){
    	if( is_admin() ) {
    		wp_register_script('EMSF_Options, plugins_url('emsforms/js/EMSF_Options.js'), array('jquery','jquery-form'), '1.0');
    		wp_enqueue_script('EMSF_Options');
    
    	}
    }

    I have just read it again, is there something in the url that you can use as a condition, try to echo $pagename and see what it returns and then?

    function Add_EMSF_Options_Scripts(){
    	global $pagenow;
    	if ( $pagenow == 'emsf_options.php' ) {
    	if( is_admin() ) {
    		wp_register_script('EMSF_Options, plugins_url('emsforms/js/EMSF_Options.js'), array('jquery','jquery-form'), '1.0');
    		wp_enqueue_script('EMSF_Options');
    
    	}
    }

    HTH

    David

    victheone

    @victheone

    The second method (using strpos to find the page name in the URL) would almost certainly work for me, but $pagenow is empty. Were you just using that as an example, or is that actually supposed to be a wordpress global var?

    I got the syntax wrong, I was developing a plugin, here is the options code snippet that may help.

    global $pagenow;
    if ( $pagenow == 'admin.php' && $_GET['page'] == 'raindrops' ) :

    So if it is an options page check the url for the $pagenow and the page will be your pagename, mine was raindrops.php so ‘raindrops’

    global $pagenow;
    if ( $pagenow == 'admin.php' && $_GET['page'] == ''emsf_options' ) :

    I have used the global $pagenow in a plugin for $pagenow == 'post.php' and $pagenow == 'new-post.php' so I know it works, not sure from which version as I always have the latest.

    If I run the wp-pagenavi options page I have:
    wp-admin/options-general.php?page=pagenavi

    That would be:

    global $pagenow;
    if ( $pagenow == 'admin.php' && $_GET['page'] == 'pagenavi' ) :

    or:

    global $pagenow;
    if ( $pagenow == 'options.php' && $_GET['page'] == 'pagenavi' ) :

    or:

    global $pagenow;
    if ( $pagenow == 'general-options.php' && $_GET['page'] == 'pagenavi' ) :

    HTH

    David

    victheone

    @victheone

    That fixed it! Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] WP 3.0 – Problems enqueueing javascript with specific target’ is closed to new replies.