functions.php

optimize seo, security and theme development
with these functions.php code snippets

How To Add A Favicon To Your WordPress Theme

Favorite Icon (favicon) is a must have these days if you want your website to be recognizable quickly. A regular internet user gets used to with favicons while browsing and you can say the major benefit of using this tiny 16×16 px image on your website is, “it saves a lot of user’s time while browsing”.

Why to use a Favicon?

Here you find a couple of primary benefits of a favicon.

- Branding: Website becomes easily recognizable by having a favicon with it.
– Look & Feel: Website gives professional look and a credibility feel if it has a distinct favicon.
– Customer Relation: This little brand identity always makes a bond with your customer’s memory.
You must be thinking where we can see the usage of favicon exactly so here you go.
– With URL in address bar
– On browser tabs for web pages
– In bookmarks toolbar or menu
– In search results

How to create a PHP function for WordPress Theme Favicon?

You can follow these simple steps to add a PHP function for WordPress Theme Favicon.

Step-1: Go to your theme’s functions.php file at this location
“/wp-content/themes/yourtheme/functions.php”

Step-2: Now create the function and action for adding a favicon to your theme in ‘functions.php’. If you already haven’t defined any custom functions before, you can add this in the end of file.

In the function above, we have defined a tag that would add favicon to the header of our WordPress theme. You just need to replace the words in CAPS with actual theme name and favicon image name. Please note that this image must be on the same path as defined in this function.

Step-3: After defining the functionality, it is time to hook this function with an event and in this case we are hooking with WordPress Header.

add_action ("hook_name_here", "function_to_add");

First variable being passed to this function defines the specific action to which function is hooked and second variable defines the function name that is to be added on hooked event.

Add this one line after closing your function code.

Heres our full code