0

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

2 Answers 2

4

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' )
    );
}
2
  • 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
    Commented Oct 7, 2012 at 19:39
  • @Volomike See edit
    – kaiser
    Commented Oct 8, 2012 at 8:17
0

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');
2
  • I just ran a test -- it does it to ALL admin pages, not just the ones for my plugin.
    – Volomike
    Commented Oct 7, 2012 at 18:37
  • An if-statement around the add_action() could solve that.
    – hampusn
    Commented Oct 8, 2012 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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