Adding CSS to custom post type admin page causes error
-
16-10-2019 - |
Question
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?
Solution
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' );
OTHER TIPS
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..