In WordPress, functions.php or the theme functions file is a template included in WordPress themes. It acts like a plugin for your WordPress site that’s automatically activated with your current theme. The functions.php file uses PHP code to add features or change default features on a WordPress site.
For example, a WordPress theme might add a bit of code to the theme’s functions.php file in order to add a new widget area to the footer, or add a custom welcome message to the WordPress dashboard. The possibilities are endless!
The functions.php file automatically loads when you install and activate a theme on your WordPress site.
Where Is the functions.php File Located?
The functions.php file location is in your theme folder.
If you want to add a code snippet to your WordPress site, adding it to the functions.php file is one option.
But it’s usually not the best way to do it. WordPress tries to separate design and functionality whenever possible. This is the reason that we have themes, which determine the design, and plugins, which determine functions.
It’s best if you’re able to change your WordPress theme without changing the way your site functions, or change your plugins without affecting your site design.
There are many WordPress tutorials that will tell you to add code snippets to your theme’s functions.php file, but that’s usually not a good idea.
If you decide to edit your functions.php file, please use extreme caution. Here are 3 reasons why editing your functions.php file is not a good idea:
- Edits to the functions file will be lost when the theme is updated.
- Edits will be lost if you change your WordPress theme.
- Making coding errors in the functions file can lock you out of your site.
Even something as simple as a missing semicolon could make your whole site disappear with an error called the “White Screen of Death”. If that does happen to you, we wrote a step-by-step guide on how to fix the WordPress White Screen of Death so you can regain access to your site.
Making a coding mistake in your functions.php file can also lead to other WordPress errors as well.
You should only edit a functions.php file if you have a child theme and the code snippets will only be used with the active child theme. For example, you may want to include snippets for custom fonts, stylesheets, or add a language translation file specifically for that particular child theme.
The Best Way to Add Custom Code Snippets to functions.php
So, if you shouldn’t edit functions.php, where are you supposed to add code snippets from online tutorials?
The simplest way is to use WPCode, a powerful WordPress code snippet plugin that makes it easy for you to add custom WordPress features using code snippets without editing your theme’s functions.php file.
While you can add your own code snippets, it conveniently comes with a built-in code snippets library where you can find some of my most popular WordPress code snippets that will help you eliminate the need of separate plugins.
For example, there are ready-made snippets that will help you keep your WordPress website secure, allow you to upload SVG images, choose your WordPress editor, and disable comments.
Once you install and activate the plugin, you can head over to the Code Snippets page and then click the ‘Add New’ button to create your first code snippet in WordPress.
This will bring you to the ‘Add Snippet’ page.
Here you can choose a code snippet from the pre-made library or add your custom code.
You need to start by entering a title for your custom code snippet, then paste your code snippet into the code box.
Be sure to also select the correct code type from the drop-down menu on the right.
To learn more about how to use WPCode, see our guide on how to add custom code snippets to WordPress.
We hope this article helped you learn all about the functions.php file in WordPress and how to add code snippets to your site! Check out the additional reading below to learn more.
- WordPress Plugin vs Functions.php file (Which is better?)
- 15 Useful WordPress Configuration Tricks That You May Not Know
- How to Easily Add Custom Code in WordPress (Without Breaking Your Site)
- How to Add Header and Footer Code in WordPress (the Easy Way)
- Beginner’s Guide to Pasting Snippets from the Web into WordPress
- How WordPress Actually Works Behind the Scenes (Infographic)
- 32 Extremely Useful Tricks for the WordPress Functions File