Code to Add a Favicon to Your WordPress Site and Dashboard
Not sure why you'd want to use an entire WordPress plugin for this easy task! Here's a custom code that adds a favicon to the front of your site and to the backend dashboard as well.
What's cool is this also pulls the valid mime type from the extension automatically, which means it works with any favicon type: PNG, JPG, SVG, ICO, etc. All you need to do is upload your image and replace the example image URL with a link to your favicon, and this code does the rest.
Simply add this at the bottom of your theme's functions.php file (example: /wp-content/themes/your_theme_name/functions.php):
// Add favicon to the frontend and backend
// Replace with your actual favicon URL
$favicon_url = 'http://example.com/uploads/files/favicon.png';
// No need to edit below this line
function generate_favicon_html() {
global $favicon_url;
$favicon_html = '';
$extension = pathinfo($favicon_url, PATHINFO_EXTENSION);
$favicon_html .= '<link rel="icon" type="image/' . $extension . '" href="' . $favicon_url . '">';
return $favicon_html;
}
function add_favicon() {
echo generate_favicon_html();
}
add_action('wp_head', 'add_favicon');
function add_admin_favicon() {
echo generate_favicon_html();
}
add_action('admin_head', 'add_admin_favicon');
?>
Remember to swap out that example favicon link with a link to your favicon image.
October 27, 2024
Hey, just wondering if the script works well with multisite setups?
November 18, 2024
Hey Freeman!
Yes it definitely works with multisite setups. Just be sure that the favicon settings are applied across the whole network. I've seen multisite networks have a few quirks sometimes so make sure to double-check your settings.
Please let me know how everything goes!