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.

How do I add a favicon that only shows during viewing of my plugin's admin panel? I mean, what event do I intercept?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

When you add your admin (sub)page, then you're (hopfully) using add_*menu_page(). You can simply save its result in a var. This var is the $hook_suffix.

Then you can simply add your callback (that adds the favicon) to the admin_head-{$suffix} hook Source Link. As the plugins screenshot shows you, there're also the load-{$hook_suffix}-hooks Source Link, which are a bit earlier.

Edit: To get better an insights and to "proof", that this hook actually exists, you can use my »current admin info« plugin, which you can download on GitHub. It shows you which globals are set, as well as which contextual hooks are available and what their names are, on the admin page you're currently viewing.

enter image description here

Example

Here's how you'd do it in a procedural way. Inside an OOP architectured piece of code, you'd probably save it into a class var and then use this var to add your favicon action/callback.

function wpse61424_register_admin_page()
{
    $hook_suffix = add_menu_page( /* add arguments */ );
    add_action( "admin_head-{$hook_suffix}", 'wpse67424_parttime_favicon' );
}
function wpse67424_parttime_favicon()
{
    printf(
         '<link rel="shortcut icon" href="%s" />'
        ,plugin_dir_url( 'favicon.ico' )
    );
}
share|improve this answer
    
I couldn't get this to work. Do you have an example? Also, are you sure this isn't deprecated in 3.4 with a better solution? –  Volomike Oct 7 '12 at 19:39
    
@Volomike See edit –  kaiser Oct 8 '12 at 8:17

You can add this to your functions.php file and or make a plugin out of it.

function volimike_admin_area_favicon() {
echo '<link rel="shortcut icon" href="http://link.to/favicon.ico" />';
}
add_action('admin_head', 'volimike_admin_area_favicon');
share|improve this answer
    
I just ran a test -- it does it to ALL admin pages, not just the ones for my plugin. –  Volomike Oct 7 '12 at 18:37
    
An if-statement around the add_action() could solve that. –  hampusn Oct 8 '12 at 8:22

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.