WordPress.org

Ready to get started?Download WordPress

Forums

add_action admin_print_scripts for a specific admin page (10 posts)

  1. futtta
    Member
    Posted 3 years ago #

    I've got me a small plugin (wp youtube lyte) which uses some external javascript (jquery plugins zrssfeed & cookie) for the presentation on the plugin's admin page.

    I am now using
    add_action( 'admin_print_scripts', 'lyte_admin_scripts' );
    to call the function that enqueues my javascript files, but this adds the JS on every admin-page which is not needed (and even unwanted).

    Based on what I read in this article on prevolac.com it is possible to specify the exact page on which the script is supposed to be loaded with something like:

    add_action( "admin_print_scripts-options.php", 'my_admin_scripts' );
    function my_admin_scripts() {
      // wp_enqueue_script...
    }

    So my question: if the admin-page which I want the JS to be included in resides at wp-admin/options-general.php?page=wp-youtube-lyte/options.php, what should the correct admin_print_scripts- suffix be? I've tried various combinations and can't seem to find the correct one?

  2. futtta
    Member
    Posted 3 years ago #

    well, that seems to be hard, no? ;-)

    just to clarify; the combinations I tried and which did not work were:

    • options-general.php
    • wp-youtube-lyte/options.php
    • wp-youtube-lyte
    • options-general.php?page=wp-youtube-lyte/options.php

    Anyone got an idea of what I'm doing wrong here?

  3. Mark / t31os
    Moderator
    Posted 3 years ago #

    For pages that are subpages of the options section, hooks are prefixed with settings_page_, so your action will need to look something like this..

    add_action( "admin_print_scripts-settings_page_YOURPAGENAME", 'my_admin_scripts' );

    Hope that helps... :)

  4. futtta
    Member
    Posted 3 years ago #

    thanks mark!

    admin_print_scripts-settings_page_wp-youtube-lyte
    admin_print_scripts-settings_page_wp-youtube-lyte/options.php

    do not seem to work however. how is the pagename defined?

  5. Mark / t31os
    Moderator
    Posted 3 years ago #

    It's defined when you add the page using add_options_page.. (fourth parameter - fifth parameter if you use add_submenu_page)..

    Can i see the code you use to register the pages?

  6. futtta
    Member
    Posted 3 years ago #

    sure;

    add_options_page( 'WP YouTube Lyte settings', 'WP YouTube Lyte', 'manage_options', __FILE__, 'lyte_settings_page');

    so that would make my pagename 'lyte_settings_page'?

  7. Mark / t31os
    Moderator
    Posted 3 years ago #

    The name you put into the fourth parameter is the hook you append to your admin_print_scripts actions..

    So your two pieces of code should look like this..

    add_options_page( 'WP YouTube Lyte settings', 'WP YouTube Lyte', 'manage_options', 'lyte_settings_page', 'lyte_settings_page');

    and..

    add_action( "admin_print_scripts-settings_page_lyte_settings_page", 'my_admin_scripts' );

    An easier way is to store the hook in a variable for easy reference, by adding your script action inside the function you're hooking onto admin menu, so you can do this...

    $hook = add_options_page( 'WP YouTube Lyte settings', 'WP YouTube Lyte', 'manage_options', 'lyte_settings_page', 'lyte_settings_page');
    add_action( 'admin_print_scripts-' . $hook, 'my_admin_scripts' );

    Hope that helps...

  8. futtta
    Member
    Posted 3 years ago #

    great, thanks a million mark! is this as far as you know documented somewhere in the codex?

  9. Mark / t31os
    Moderator
    Posted 3 years ago #

    You're welcome.. :)

    You can find a similar example on the action reference page.
    http://codex.wordpress.org/Plugin_API/Action_Reference

  10. Brayne
    Member
    Posted 3 years ago #

    I needed to add some scripts and css to the users profile page and used this code and thought I'd share in case someone else needs clarification...

    This registers a javascript and css file and uses these files on the user's profile page and only loads on the user's profile page.

    add_action('admin_init','my_cool_options');
    add_action('admin_print_scripts-profile.php', 'my_cool_styles');
    
    function my_cool_options() {
    		wp_register_script( 'mycoolscript', getContentURL() . '/my_cool.js' );
    		wp_register_style( 'mycoolstyle', getContentURL() . '/my_cool.css' );
    	}
    
    function my_cool_styles() {
            wp_enqueue_script( 'mycoolscript' );
            wp_enqueue_style( 'mycoolstyle' );
    	}
    
    function getContentURL() {
    		$mupluginsurl = WP_CONTENT_URL."/mu-plugins";
    		return ($_SERVER['HTTPS'] != "on") ? $mupluginsurl : str_replace("http", "https", $mupluginsurl);
    	}

    I used a custom function for calling the mu-plugins directory as this is where I place the files for a multi-site environment.

    Hope this helps someone.

Topic Closed

This topic has been closed to new replies.

About this Topic