Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this bit of code in my Meteor Slides plugin that loads a stylesheet on the admin pages of just the slides custom post type:

    add_action('admin_head', 'meteorslides_admin_css');

function meteorslides_admin_css() {

    global $post_type;

    if (($_GET['post_type'] == 'slide') || ($post_type == 'slide')) :

        echo "<link type='text/css' rel='stylesheet' href='" . plugins_url('/css/meteor-slides-admin.css', __FILE__) . "' />";

    endif;

}

This code works fine and hasn't cause any issues, but in debug mode, it does cause this error that I'd like to resolve:

// Notice: Undefined index: post_type in C:\Program Files\xampp\htdocs\slides\wp-content\plugins\meteor-slides-1.3\meteor-slides-plugin.php on line 476

I haven't been able to fix this error, does anyone have any suggestions, or a different way to add a stylesheet to the admin pages of a certain post type?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

You need to check for the presence of 'post_type' as an index of $_GET before using it:

if ((isset($_GET['post_type']) && $_GET['post_type'] == 'slide') || (isset($post_type) && $post_type == 'slide')) :

Also, you should be using the wp_enqueue_style function instead of echoing your stylesheet at 'admin_head':

wp_enqueue_style( 'meteor-slides-admin', plugins_url('/css/meteor-slides-admin.css', __FILE__), array(), '1.0' );

More information on wp_enqueue_style here.

share|improve this answer
    
Thanks so much John, it makes sense that I need to check that before using it, and this totally solved the error that I was getting. –  JLeuze Oct 21 '10 at 21:27
    
And thanks for the heads up on wp_enqueue_style, I am using wp_enqueue_script on all my JavaScript, but I was unaware that this existed for stylesheets. It didn't work for me on the first go, but I'll have to read up on it and figure it out. –  JLeuze Oct 21 '10 at 21:29
    
I think I know why it didn't work. I thought you were hooking onto admin_init; admin_head is too late to enqueue a style. I'd suggest hooking onto 'admin_enqueue_scripts'. It runs before the styles and scripts are printed, and shouldn't change any of the functionality of your code. –  John P Bloch Oct 22 '10 at 12:21
    
Thanks again John, running wp_enqueue_style on admin_enqueue_scripts worked perfectly. Now I've got all the stylesheets in my plugin properly enqueued! –  JLeuze Oct 22 '10 at 15:33
add comment

There are hooks for adding actions to specific pages.. and additionally various variables that hold data about the current page, the post type, the parent file, etc..

admin_print_styles would be the correct hook to use for enqueuing styles, and for the edit page your action could look a little something like this..

add_action( 'admin_print_styles-edit.php', 'example_function' ); // Will work for any post type, check inside the callback function
//add_action( 'admin_print_styles-post-new.php', 'example_function' ); // Example
//add_action( 'admin_print_styles-edit-tags.php', 'example_function' ); // Example
function example_function() {
    global $typenow;
    if( 'my_custom_type' == $typenow )
        wp_enqueue_style( 'meteor-slides-admin', plugins_url('/css/meteor-slides-admin.css'), array(), '1.0' );
}

In this case the hook is edit.php, each admin page has a similar hook. WordPress admin_header.php basically has a set of actions that fire, which look like so..

do_action('admin_enqueue_scripts', $hook_suffix);
do_action("admin_print_styles-$hook_suffix");
do_action('admin_print_styles');
do_action("admin_print_scripts-$hook_suffix");
do_action('admin_print_scripts');
do_action("admin_head-$hook_suffix");
do_action('admin_head');

And admin.php sets the hook suffix with the following..

$hook_suffix = '';
if ( isset($page_hook) )
    $hook_suffix = $page_hook;
else if ( isset($plugin_page) )
    $hook_suffix = $plugin_page;
else if ( isset($pagenow) )
    $hook_suffix = $pagenow;

All the core code aside, the example function i posted further up is a working example you can use for targetting the edit posts(custom type or not) screen....

Hope that helps..

share|improve this answer
    
Thanks for the info t31os, I'll try it out! I tried to use admin_print_styles before, but had trouble getting it to work with custom posts types. –  JLeuze Nov 9 '10 at 4:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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