In the world of WordPress, functions.php file has always been one of the most popular files where people make edits to add new features to their website, without even understanding the best coding practises and what are the things they are doing wrong. The reason functions.php file is so popular is because in almost every forum and help article whenever there is some new feature needed to be added, or some custom codes for some custom features the user wants, all of these sites ask the users to add a small snippet of code to their theme’s functions.php file and start enjoying the custom features.

But this is wrong, not in terms of functionality but in terms of programming, scalability and debugging. Today in this article I’ll show you what the functions.php file actually is, why it was built for and why you should not be throwing all of your site custom codes there. On top of this, I will also show you the better way of handling your custom codes, the way you should have taken on the first place. So, let’s get started. But before we begin further, let’s take a look at the things I’m going to cover in this article.

What is the actual job of functions.php in WordPress?

Before I dig deeper into why you shouldn’t add all of your custom feature codes into the functions.php file, the first thing you need to understand is the actual job of that particular file. Why that file existed in all WordPress theme in the first place? If you understand that, then it will become more easier for you to see why we shouldn’t add all of our custom codes into that file.

The functions.php file is the heart of WordPress themes because it holds various functions to make your theme work just the way you want it. You see, WordPress is a PHP based highly efficient CMS which relies on dynamic web building. That means, you can change any aspect of your site very easily. Like changing contents of your site. In case of static HTML based websites, these things are very complex as you need to dig inside each HTML file to make those certain changes.

But as WordPress is a dynamic CMS, it handles everything dynamically. Now to handle things dynamically, sometimes you need to add custom functions inside your theme, so that it grab the data from the database and then dynamically put it in the exact spot you want it.

Fortunately WordPress comes with a ton of in-build functions to work with, but if you need more customized way of handling things, you must need to add your custom functions inside the functions.php file and then call that function within your WordPress theme code, exactly where you want it. That’s the actual job of the functions.php file in WordPress.

In short, functions.php file allows you to create your own custom functions that you can use inside your WordPress theme to behave your theme exactly the way you want it to. So, this file is mainly for supporting your theme development and not for adding custom features to your site. Now by custom features, I mean those things which has nothing to do with the visual implementation of your site.

As functions.php hold various functions, you can actually use it to put your custom codes to add some features you want in your website. But again this is not the right way to do thing and it can give you a huge pain in the long run in terms of scaling or maintaining your website or it’s code.

As I said earlier, the functions.php file is basically a part of your theme that is supposed to handle only the visual implementation of your website. That is why it is called Theme. For features that has nothing to do with your site’s overall visual aspect, there is another great feature that WordPress provides, is called Plugins.

I know you might a little bit confused right now thing how can we differentiate between what custom code is theme specific and which one is not. So, let me make it dead simple to your guys. Theoretically, if you disable all of your plugins, your site should still look almost same as it was before but you may lose some custom features like social share icons, ads showing up etc. So, if you are adding any code in your site which add more functionality to it, it should always be a plugin code and not a theme specific code.

Now at this point you might be thinking, what’s the problem and why I’m rambling about it so much if things are still working. Well, just because things are working, doesn’t mean it has been done in the right way. Trust me on a small-scale you might not see any problem with adding all of your custom codes within the functions.php file, but on the long run, if your site got too many concurrent visitors or you need to scale your site or you need to maintain/edit your custom codes, this approach is soon going to be a nightmare for you.

You see, WordPress is a very smart system and it executes different things with different priority. So, when you are adding custom feature codes within your functions.php WordPress treats it as a part of the theme and executes it with the entire theme. This is bad, very bad.

Then, what is the right way of adding custom feature codes into WordPress?

Well, as I explained above, WordPress has two key components within it. One is Theme and the other is Plugin. Where a theme should only concentrate on the codes that is associated with the visual look and feel of the website, plugins on the other hand concentrate on incorporating custom features into your sites that you want. So, any custom code that has nothing to do with your site’s basic visual appearance, just use those codes as an individual plugins and not overcrowd your theme’s functions.php file.

If you are asking yourself about the benefits of this approach, well there are mainly two key benefits of this approach.

The first benefit is easy scalability. You see, if you add each of your custom feature code as a separate plugin in your website, it will be easier for you in future when you need to update something within that code block. You don’t have to dig inside functions.php file and look for that exact function within thousands and thousands lines of code. Instead, you can just easily go to that exact plugin and make your changes inside that code. In this way you will know whatever changes you are making is specifically targeted towards the custom feature that code block is providing. Moreover you can add that particular plugin file in your Github repo and easily collaborate with others as you do not need to share your entire theme or and FTP access to your entire site. This is the smarter way of doing things.

The second benefit is easy debugging. Coding mistakes is a daily thing for all programmers. Whether you are just starting to code or you are a professional programmer. Trust me there is no shame in it. When you start writing custom codes, you will always face various errors in the beginning and then you fix them to make your code actually work the way you want to. This is the normal procedure of coding. Now if you have added your custom codes inside the functions.php file, many times your error log file won’t be able to point out the exact origin of the problem and throws vague errors which you then spend hours to figure out what is causing it.

Also many times when you add some custom codes in your site it may break some other features in your site due to the compatibility with other plugins you have on your site. Now if you take the functions.php approach of doing things, then the only thing you can do at these points is commenting or removing all of your custom codes that you have added to check if the problem has been fixed or not. Honestly this is a very cumbersome and idiotic way of doing things.

On the other hand if you use the plugin approach of adding your custom codes into your site, you can just simply disable your custom-made plugins from the WordPress plugin menu and you can easily figure out which plugin is causing the mess. Then you can dig inside that particular plugin’s code so see what wrong code that you have written causing the issue. Doesn’t it feel more easier to you?

Also if you use the plugin approach of adding your custom codes into your WordPress site, in 99.999% cases your server error log will show you the exact plugin file path that is causing issues. So, in short, using the plugin approach of adding custom codes into your WordPress site not just the right way to do things, but it makes debugging problems in your site super easy.

So, how to add my custom feature codes as WordPress plugin?

Most webmaster who uses WordPress on a daily basis has a feeling that creating plugins are extremely complicated and hard. Trust me this is not true always. Yes, if you are making a generalist plugin with admin menu, settings pages etc. then yes it is pretty hard if you don’t know how to do those things. But for simple code blocks that you are supposed to add within your functions.php file, instead adding them as a plugin is dead simple. Let me show you how to do it.

The first one is using the proper comment block at the top of your code, exactly the way it has been shown below. It will help WordPress to understand that this is a plugin, along with some basic information about that plugin, like the plugin’s name, description etc.

The second thing you need to do is create a folder within /wp-content/plugins/ folder, with your plugin’s name and put your php file inside that folder. Let’s say you are adding a custom code to add AdSense ad code after the first paragraph for each of your blog post. So, instead adding that custom code block within functions.php file, you can create a folder inside /wp-content/plugins/ with a name ad-after-first-paragraph.

The third and last thing you need to do is create a php file within the plugin folder your have created and put your custom code block there. Now generally as per good coding practice we always use the same name for the plugin folder and the actual plugin php file. So, if I take the previous adsense example, as we have already created out plugin folder ad-after-first-paragraph within /wp-content/plugins/ folder. Now create the actual php file within which we are going to add our custom code block and save the file as ad-after-first-paragraph.php. That’s all you need to you.

But no matter what make sure your plugin’s php file starts with the following comments before adding any custom codes within it, so that WordPress can identify the main plugin file along with it’s name, description and other things. Just copy paste the following in your plugin’s php file and change the values like plugin name, description, author name etc.

After this is done, just head over to the plugins section within your WordPress admin panel and enable your plugin. That’s all you need to do. But now as you have added your custom codes as separate plugins, you can easily deactivate and modify them individually.

Conclusion

Having a WordPress site with various custom features is great but you need to make sure you implement your code the right way so that in future if you ever need to debug or scale your website’s code you can do that easily without any extra hassle. Adding all custom codes to the functions.php might be easier as you don’t need to create new folders for your plugins or no need to add extra plugin header texts, but if you look broadly you will see that this 2 minutes extra work can actually save a lot of your development, coding or debugging time.

Feedback

So, what approach do you use for your custom codes. the functions.php apprach or creating individual approach? I would love to know your thoughts on this matter. ALso if you like this article, please do share it with other WordPress enthusiasts, so that they can also code their site the right way.

Also if you want you can connect with me over twitter with my twitter handle @iSaumya. I’m looking forward to hear your thoughts on this and if this small piece of code helped you anyway. 🙂

1 Comment

Very nice article !! I am very impressed the way you’ve designed your site. It will be really helpful if you make a tutorial about how to create those header & footer mouse hover effect for a site. Thanks in advance. Please do help with this !!

Subscribe To My Newsletter

You are almost there! Just check your Inbox to confirm the Subscription & you are all set.

A Little About Me

Hi, I'm Saumya Majumder, an engineer, tech enthusiast & programmer. Here in this blog I write mostly about starting & managing a blog, WordPress, Social-media, SEO and Making Money online, Adsense, latest tech products and software and many more thing. If you are also into the tech arena, you will definitely love the content of this blog (I hope).

Subscribe To My Newsletter

You are almost there! Just check your Inbox to confirm the Subscription & you are all set.

Shares

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.