The do_faviconico WordPress PHP action fires when serving the favicon.ico file.
Usage
add_action('do_faviconico', 'your_custom_function'); function your_custom_function() { // your custom code here }
Parameters
- None
More information
See WordPress Developer Resources: do_faviconico
Examples
Custom Favicon for Your Site
Serve a custom favicon from your theme’s images folder:
add_action('do_faviconico', 'my_custom_favicon'); function my_custom_favicon() { $favicon_url = get_template_directory_uri() . '/images/favicon.ico'; header('Content-Type: image/vnd.microsoft.icon'); readfile($favicon_url); exit; }
Custom Favicon for Admin Pages
Serve a different favicon for admin pages:
add_action('do_faviconico', 'admin_custom_favicon'); function admin_custom_favicon() { if (is_admin()) { $favicon_url = get_template_directory_uri() . '/images/admin-favicon.ico'; } else { $favicon_url = get_template_directory_uri() . '/images/favicon.ico'; } header('Content-Type: image/vnd.microsoft.icon'); readfile($favicon_url); exit; }
Serve Different Favicons Based on User Role
Serve different favicons for different user roles:
add_action('do_faviconico', 'user_role_based_favicon'); function user_role_based_favicon() { if (current_user_can('administrator')) { $favicon_url = get_template_directory_uri() . '/images/admin-favicon.ico'; } else { $favicon_url = get_template_directory_uri() . '/images/user-favicon.ico'; } header('Content-Type: image/vnd.microsoft.icon'); readfile($favicon_url); exit; }
Serve Favicon Only for Logged-in Users
Serve favicon only for logged-in users, and show a default one for others:
add_action('do_faviconico', 'logged_in_user_favicon'); function logged_in_user_favicon() { if (is_user_logged_in()) { $favicon_url = get_template_directory_uri() . '/images/favicon.ico'; } else { $favicon_url = site_url() . '/favicon.ico'; } header('Content-Type: image/vnd.microsoft.icon'); readfile($favicon_url); exit; }
Serve Favicon Based on Custom Field Value
Serve different favicons based on a custom field value:
add_action('do_faviconico', 'custom_field_based_favicon'); function custom_field_based_favicon() { if (is_single()) { $custom_favicon = get_post_meta(get_the_ID(), 'custom_favicon', true); if ($custom_favicon) { $favicon_url = wp_get_attachment_url($custom_favicon); } else { $favicon_url = get_template_directory_uri() . '/images/favicon.ico'; } } else { $favicon_url = get_template_directory_uri() . '/images/favicon.ico'; } header('Content-Type: image/vnd.microsoft.icon'); readfile($favicon_url); exit; }