0

I'm trying to load a stylesheet for a particular admin submenu page. The code below is what I came up with after looking up admin_enqueue_scripts in the codex:

function my_function($hook) {
    if ( 'themes.php?page=page-name' != $hook ) {
        return;
    }
    wp_enqueue_style( 'custom-style', get_template_directory_uri() . 'custom-page/style.css' );
}
add_action( 'admin_enqueue_scripts', 'my_function' );

As you can see I'm trying to load it on a submenu page. Not just on 'themes.php', but on a custom page I've created called 'page-name'. The code works if 'themes.php' != $hook and loads on 'themes.php'. 'themes.php?page=page-name' != $hook however does not load on 'themes.php?page=page-name'.

Any idea on how to go about this? Thanks in advance!

EDIT: This is how the page is declared

function my_function_settings_page_init() {
    $theme_data = wp_get_theme();
    $settings_page = add_theme_page( $theme_data->get( 'Name' ). ' About', $theme_data->get( 'Name' ). ' About', 'edit_theme_options', 'page-name', 'my_function_settings_page' );
    add_action( "load-{$settings_page}", 'my_function_load_settings_page' );
}

function my_function_load_settings_page() {
    $_POST["my-function-settings-submit"] = '';
    if ( $_POST["my-function-settings-submit"] == 'Y' ) {
        check_admin_referer( "my-function-settings-page" );
        my_function_save_theme_settings();
        $url_parameters = isset($_GET['tab'])? 'updated=true&tab='.$_GET['tab'] : 'updated=true';
        wp_redirect(admin_url('themes.php?page=page-name&'.$url_parameters));
    exit;
    }
}
3
  • how have you declare this page called "page-name" ?
    – mmm
    Commented Aug 29, 2015 at 13:19
  • Edited the question. Code is based on this tutorial: smashingmagazine.com/2011/10/…
    – kath
    Commented Aug 29, 2015 at 13:48
  • ok, then you can try my answer below
    – mmm
    Commented Aug 29, 2015 at 13:59

2 Answers 2

1

Ok, I figured it out: If it's a submenu page, the $hook looks like this: appearance_page_page-name instead of going by URL. So the complete code would be this:

function my_function($hook) {
    if ( 'appearance_page_page-name' != $hook ) {
        return;
    }
    wp_enqueue_style( 'custom-style', get_template_directory_uri() . 'custom-page/style.css' );
}
add_action( 'admin_enqueue_scripts', 'my_function' );
0

if you have declared the page with add_theme_page(..., you can add the stylesheet in the hook load-appearance_page_page-name or admin_print_styles-appearance_page_page-name.

for information, these are some hook which are fired when you load the page (it's not the whole list but the most interesting for your question)

plugins_loaded
init
admin_init
current_screen
load-appearance_page_page-name
admin_enqueue_scripts
admin_print_styles-appearance_page_page-name
admin_print_styles
admin_print_scripts-appearance_page_page-name
admin_print_scripts
wp_print_scripts
admin_head-appearance_page_page-name
admin_head
admin_notices
all_admin_notices
appearance_page_page-name
in_admin_footer
admin_footer-appearance_page_page-name

they are in the order that they append

2
  • Thanks. I've tried both 'admin_print_styles-appearance_page_page-name' and 'admin_print_scripts-appearance_page_page-name' and nothing is happening. Could it be this part if ( 'themes.php?page=page-name' != $hook ) that is causing and issue?
    – kath
    Commented Aug 29, 2015 at 14:03
  • the hooks which ends with appearance_page_page-name are fired only on this page. then you don't need other tests, just one line wp_enqueue_style(...
    – mmm
    Commented Aug 29, 2015 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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