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
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.
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' )
);
}
-
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?– VolomikeCommented Oct 7, 2012 at 19:39
-
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');
-
I just ran a test -- it does it to ALL admin pages, not just the ones for my plugin.– VolomikeCommented Oct 7, 2012 at 18:37
-