Did you ever wonder about the ways of using your own custom functions in WordPress? The first obvious answer that would pop up in anyone’s mind would be WordPress plugins.
But there’s a second option available as well. And that’s using the WordPress functions.php file.
Today we’ll be talking about some key points as to how you can take advantage of the functions.php file in WordPress. We’ll be talking about the ways you can find access them, how to add custom functions, and a few important tips here and there.
Today’s quick guide is meant for those who are getting just getting into the whole ‘WordPress’ sphere and want to get their hands dirty. With that said, we’ll start with an obvious question:
Why would I want to us functions in the first place?
The very first thing anyone should know about WordPress is that it’s built on a programming language called PHP. So, naturally, files like the functions.php and other core WordPress functions are in PHP. This means, if you want your own functions to work, you’ll need to have some understanding of PHP.
Unfortunately, that’s a topic for another time.
For now, let’s try to answer the question we’ve put forth.
Functions in coding have a very distinct advantage. They CAN be used multiple times. As a matter of fact, all you have to do is create a function and call it wherever you want to use it inside your code. And that makes it a REUSABLE asset.
Now an argument can be made about WordPress plugins as well.
You should be able to do the same thing with plugins right?
Yes. In general, novice users will find it better to use plugins for such purposes. However, if you ever plan to move to a different plugin with different settings, you can’t really bring back the old settings. Once you remove it from your WordPress website, you may have to build everything from the ground up with your new plugin.
Then there’s another obvious question:
I don’t know PHP? What should I do now?
You’re forgetting that this is the age of information. Whatever code you need is mostly available on the internet (with proper documentation). As a matter of fact, we have a tutorial where we show you how to change your add to cart button using your theme’s functions.php file [we’ll be discussing how to add code to your theme’s functions.php file as well].
Another key fact to remember is that too many WordPress plugins can slow your site down. If you have more than 20-plugins running on your website [suggested by the expert community], then chances are your site will slow down, affecting your search rankings. However, reusing code will have a minimal effect, making it the better option if you are willing to use custom functions on your WordPress website.
Where do I find the WordPress functions.php file?
There are a couple of ways you can access the functions.php file. However, before accessing it, there are a couple of things you need to know about:
What happens if I try to edit the WordPress functions.php?
If you are new to this, JUST DON’T!
You see there’re mainly 2 functions.php files in WordPress. One is associated with the core files, and the others are basically associated with the themes you use [used to enable theme functions-things your theme can do!]. The core functions.php file has a different style in contrast to the theme’s functions.php file.
If you have a piece of code and don’t know where to put it, seek help from an expert. Because if you somehow misplace anything on the original code, chances are that you’ll be locked out of your website for good!
Where are the functions.php files located?
Here are the locations of the functions.php files:
- If you want to observe the core functions.php file then you’ll have to access your WordPress website’s cPanel and go to the File Manager. Once there go to public_html>wp-includes. If you scroll down, you’ll find the functions.php file.
- If you want to access your theme’s functions file from the cPanel, go to File Manager>themes>’Active Theme’. Here ‘Active Theme’ means the theme that’s currently active on your WordPress website. Pretty much every theme you install will have a function.php file.
- For those of you who are running a WordPress website on your localhost using XAMPP, go to xampp>htdocs>’Active Site’>wp-content>themes>’Active Theme’>functions.php. If you have a code editor like Visual Studio Code, you can open the file in the editor.
Again, ‘Active Site’ and ‘Active Theme’ are the names of the site you’re running on localhost and the theme that’s active on the site [as you can have multiple WordPress themes installed on your site]. Make sure to check out this video on how to use xampp to run WordPress websites locally on your machine.
How to add custom functions to WordPress functions.php?
There are two ways you can do this:
Use ‘My Custom Functions’ plugin
Installing the plugin is the same as installing any other plugin on WordPress. Just Install and Activate the Plugin from your WordPress dashboard.
Once you’ve done that, you’ll see the ‘PHP Inserter’ option under the Settings menu in your WordPress dashboard. We’ll be using the following bit of code to add an excerpt to WordPress pages:
add_post_type_support( 'page', 'excerpt' );
***The above-mentioned code adds excerpt support to the WordPress pages. There are other important functions like add_theme_support(), where the modules of your theme are told to function. Then there are the activate_plugins() and delete_plugins() which can either activate plugins on your WordPress website or remove plugins completely from the WordPress directory.
In order to add this code, you’ll have to:
- Go to the Settings>PHP Inserter.
- Add the code.
- Toggle the ‘ON’ button to the right.
- Hit ‘Save Changes’
Use the ‘Theme Functions’ file
You can use the theme functions.php file to insert the previously mentioned code to show the excerpt in WordPress pages. To do that:
- Go to Appearance>Theme Editor.
- Choose the theme you’re using from the dropdown to the right.
- Select ‘Theme Functions’ which is the WordPress functions.php file for your theme.
- Paste the code.
- Finally, click on ‘Update File’
In both cases, you’ll see an excerpt box added to WordPress pages.
Caution: Make sure to copy the code correctly. The file may update successfully, but you’ll not be able to see any results.
This discussion on WordPress functions.php has been a small one. But we hope to bring a follow-up to this content where we share some custom functions you can add to the functions.php WordPress theme file.
Let us know if you want a follow-up. Do sound off in the comments below and tell us what type of content you’d like to see from us next.