I’ve setup an “album” custom post type and want to add an icon on the index page where the RED circle is (see image below; from wptheming.com).
Here’s my function to replace the icon. The CSS is what I found when I inspected the icon element with firebug:
add_action('admin_head', 'album_foo');
function album_foo() {
global $post_type;
?>
<style>
<?php if ($post_type == 'album') : ?>
#icon-album.icon32.icon32-posts-album {
background:url('<?php get_template_directory_uri(); ?>/images/album32x32.png') no-repeat;
}
<?php endif; ?>
</style>
<?php
}
I also tried this CSS in the function:
#icon-edit.icon32-posts-album {
background:url('<?php get_template_directory_uri(); ?>/images/album32x32.png') no-repeat;
}
I can’t figure out what my CSS is missing, but my icon isn’t showing.
If when using firebug you are seeing
Then you’re passing the string
'<?php get_template_directory_uri(); ?>/images/album32x32.png'
to the background property in your css file – and they don’t execute php code.If the image and CSS are in a theme folder (or at least the CSS file is in some directory above/with the image) then you can use relative urls.
For those who are developing plug-ins for release and do not want to enqueue some CSS file for one simple line…
Unless your server has been specifically configured to do so, PHP won’t work in a .css file. It looks like that is what you are trying to do. Relative URLs work fine in a stylesheet, in contrast to most parts of WordPress. Try using a relative URL as a path to your icon.
Edit:
It isn’t
#icon-album
. It is#icon-edit
. Thatid
doesn’t change with post types, at least not with the ones I tried. Theclass
es change. Thatid
appears to stay the same.I moved the
<style
tag into the conditional since there is no need to print an empty tag and with debugging on I was gettingWARNING
s printing instide the<style>
tag which could break it. I also added a check to see if$_GET['post_type']
is set before trying to use it.I figured it out!
When WordPress looks for the 32×32 icon on custom post index/edit/add pages, it starts by looking in:
because that’s where the WordPress 32px icons are located. But CSS references are usually relative to the location of your stylesheet. So when adding the location of your icon to CSS, FIRST, you have to use /wp-admin as your reference point, so it will be different than most of your other CSS references. Then use those relative URLs to get to the directory where your icon is located.
Your CSS for a custom post type 32px icon should look like this:
Hope this helps somebody else!