The WordPress functions.php File +5 Things You Can Do With It
functions.php file is one of the most central files of any WordPress installation. It can control much of the functionality and behavior of your website. However, beginners often don’t understand what the file does and what they can use it for. This article will change that.
In the following, you will learn what the
functions.php is, where to find it, and what it can do. After that, I will talk about the file’s pros and cons and close the article with a few example code snippets that you can input into
functions.php to enhance your WordPress website.
What is functions.php and what does it do?
functions.php file is part of pretty much every WordPress installation. More specifically, you can usually find it in the directory of every theme installed on your site (including child themes).
So what does this ubiquitous file do?
functions.php file is kind of like a theme-dependent plugin. You can use it to add any kind of functionality to your site (like plugins do). However, because
functions.php is part of your theme, your changes will only be active as long as the theme is, whereas plugins work no matter which theme you’re using.
To do its job,
functions.php can contain PHP code as well as native WordPress functions. If you know your way around things like theme hooks, you can even use
functions.php to create your own hooks!
Because it can contain all types of code snippets the functions file is very powerful.
Pros and cons of using the WordPress functions.php file
functions.php basically works like a plugin. However, it doesn’t just do one specific thing. Instead, the file can be a collection of code for many different purposes. For example:
- Creating widget areas
- Adding new image sizes to your site
- Changing the text of the ‘read more’ link
- Adding custom fonts
As you can see, the file is extremely flexible. For that reason, if you want to make small changes to your site, the WordPress
functions.php file is the perfect way to accomplish that.
On the other side, it also has its limits. First and foremost, if there is any small mistake in the file (such as a missing semicolon) it can take down your entire site.
Scary, I know.
Plus, if it’s the only way you make changes to your site, the file can quickly become chaotic and confusing.
Finally, as you learned earlier, if you ever switch WordPress themes, you’ll lose the changes in your
functions.php file. If you think you’ll be switching themes soon, or are adding functionality that you want to work independently of your theme, you may be better off using the Code Snippets plugin:
This plugin gives you a simpler, theme-independent way to add code snippets to
Example functions you can use on your site
Alright, now that you know all about this important file, you can start using it. Below, you will find a few code snippets that you can copy and paste to your theme’s functions file (with site-specific customizations, of course). The best way to use them is to paste them at the end of your
1. Add Google Analytics to your site
Adding Google Analytics to your site is always a good idea. It will help you understand your audience better and track their behavior on your site so you can improve it.
Usually, you would include Google Analytics on your site via a plugin, however, it’s also possible to do it manually via the WordPress functions.php file. Here’s what that looks like:
To make the code snippet work, you need to replace everything between
<script async... and the final
</script> with your own Google Analytics tracking code.
2. Hide specific WordPress login errors
When somebody tries to log into your site with faulty information, WordPress will tell that person whether the problem is an invalid username or an invalid password:
Unfortunately, this can make it easier for hackers to get into your site. For that reason, you can use the code snippet below to change the error message to something less revealing.
Just change the text between the
" " to whatever you want the message to say.
3. Display the number of words in an article
Sometimes it’s useful to show the number of words in your posts. That way, visitors have the opportunity to decide whether they want to invest their time into the entire article or not.
First, add the following to your
The code above strips the HTML tags from your content and then counts the leftover words.
After that, you need to add
echo word_count(); wherever you want the number of words to show up. For example, I added the following line to the entry meta section of my post template.
In the code above, I wrapped the number of words in an HTML section (in order to change its appearance if necessary) and added some text around it. The actual number is displayed where it says
word_count(). The command echo simply tells the browser to display everything on the page.
After that, I looked into my child theme (it’s important that you use a child theme) for the file that is responsible for displaying posts on my site. In Twenty Seventeen, that is content.php, which is located in the theme folder under template-parts > post. However, it will probably be different in yours.
To find out which template file your theme is using, you can install the What The File plugin. It will display which file and template parts are used for displaying the page or post you are currently on.
Finally, I simply located the entry-meta section in my file and pasted the code right before the section closes. In Twenty Seventeen, that ends up looking like this:
Here is the result:
4. Add estimated reading time to posts
Instead of showing the word count, you can also go one step further and display the estimated reading time. To do this, you just need to add some extra code to the example above:
What this snippet by Matt Birchler does is take the number of words and divide it by a reading speed of 200 words per minute (that happens where it says
$word_count / 200, simply change this number if you think your readers might be slower or faster). It then outputs the results with minute or minutes at the end.
You can add it to your site the same way as the word count in the last example. The only difference is that you replace
reading_time() in your code. Here’s how I did it:
And this is is what it ended up looking like on the page:
5. Delay posts from being sent to RSS
Our final example is a way to keep posts from immediately going to your RSS feed when you hit Publish. That can be a good idea because it gives you time to correct those typos you only see once you have already sent the post out to the world.
The problem: while on your site you can simply change any mistakes, once your post is out on RSS, it’s out there. However, no longer! Use the snippet below to keep this from happening.
In this example, the number of minutes the post is delayed by (the part after $wait) is set to 10. Feel free to change it to whatever timing you find appropriate.
The WordPress functions.php file is a powerful tool to make changes to your website. If you know your way around it, there is little you can’t do.
Above, I have explained what the WordPress functions.php is, what it is used for and its advantages and disadvantages. I have also given you plenty of examples so you can grasp the possibilities of this file.
From here, it’s up to you to look into what you need on your site and how to make it happen. The functions file will help you achieve it.