In WordPress, functions.php is a theme functions file that acts like a plugin for your WordPress site and is automatically activated by your current theme.
A functions.php file is included with all WordPress themes and uses PHP code to add new features or change the default features on a WordPress site.
For example, a WordPress theme might add a bit of code to the theme’s functions.php file 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.
Editing the functions.php file using custom code allows you to add post types, taxonomies, shortcodes, and more to improve your website.
Why You Normally Shouldn’t Directly Edit the functions.php File
The functions.php file is located in your theme folder and advanced users can add code snippets by directly editing the file.
There are many WordPress tutorials that tell you to add code snippets directly to your theme’s functions.php file, but that’s usually not a good idea.
WordPress tries to separate design and functionality whenever possible. This is a good approach, and it’s often best to use WordPress themes to customize the way your site looks, and plugins to determine how it functions.
Also, making a coding mistake in your functions.php file can lead to WordPress errors or cause the White Screen of Death that makes your website disappear. These errors can be as simple as a missing semicolon, so anyone editing the functions.php file should use extreme caution.
In summary, there are 3 reasons why it’s not a good idea to edit your functions.php file:
- 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.
However, you may wish to edit the functions.php directly to make changes to one specific theme, such as adding custom fonts or a language translation file.
In that case, we recommend you use a child theme.
The Best Way to Add Custom Code Snippets to functions.php
So, if you shouldn’t directly edit the functions.php file, how 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.
You can also subscribe to our YouTube channel for step-by-step WordPress video tutorials, and follow us on Twitter and Facebook for the latest updates.
Additional Reading
- How to Add Header and Footer Code in WordPress (the Easy Way)
- 15 Useful WordPress Configuration Tricks That You May Not Know
- How WordPress Actually Works Behind the Scenes (Infographic)
- WordPress Plugin vs Functions.php File (Which is Better?)
- How to Easily Add Custom Code in WordPress (Without Breaking Your Site)
- Beginner’s Guide to Pasting Snippets from the Web into WordPress
- 46 Extremely Useful Tricks for the WordPress Functions File