Support » Plugins » Hacks » add_action admin_print_scripts for a specific admin page

  • 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 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?

Viewing 9 replies - 1 through 9 (of 9 total)
  • 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?

    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… 🙂

    thanks mark!


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

    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?


    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’?

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


    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…

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

    You’re welcome.. 🙂

    You can find a similar example on the action reference page.

    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_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.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘add_action admin_print_scripts for a specific admin page’ is closed to new replies.