0

I would like to load a stylesheet on only 3 admin pages:

  • post.php?post=7&action=edit
  • post.php?post=10&action=edit
  • post.php?post=18&action=edit

I was trying with admin_enqueue_script but it doesn't seem to be capable of such specificity (see my related wpse post), which led me to get_current_screen. But all the examples and articles I find about get_current_screen refer to using it with a plugin and/or plugin options page:

I would simply like to use get_current_screen in my functions.php to determine if the admin page loaded is 1 of the 3 pages and if so load a css file.

A simple example would be very helpful (php novice here). Thank you for any help.

2

I haven't tested this but theoretically you could do something like this (Using the Codex as an Example):

function load_custom_wp_admin_style() {
    $pageArr = array(1, 2, 3);
    if(isset($_GET['post']) && in_array($_GET['post'], $pageArr)){
        wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
        wp_enqueue_style( 'custom_wp_admin_css' );
    }
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );

We get the current screen and set the page IDs we would like to include any scripts or styles. We then make sure that there is a $_GET['post'] set which holds our post->ID and we also want to make sure we're in the correct post type (though you could probably skip the whole current screen thing entirely and just use $_GET). We then check to see if the current post ID is in our array of accepted IDs and if so: enqueue.

  • Tried it. Seems to work perfectly! Thank you so much. Could I trouble you for an example without "the whole current screen thing" as you mentioned, just using $_GET? – codeview Aug 28 '14 at 21:42
  • @codeview Glad to hear it! I've updated my answer and removed the references to get_current_screen(). If this works, don't forget to click the checkmark to show others that the question has been answered! – Howdy_McGee Aug 28 '14 at 21:44
  • That works too! This is very helpful. If I don't need to use get_current_screen I will use your edited code but I have copied the pre-edit for reference since you removed it. – codeview Aug 28 '14 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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