Best practices to add custom functionalities to a WordPress site
There's a right and a wrong way to add custom functionalities to your WordPress site. In this article go over the different options you have and the best practices in web development.
WordPress is the most popular Content Management System in the world. Almost 40% of the websites on the internet are built using WordPress. It is simple to install and use, and it includes a vast array of themes and plugins that allows you to customize your website at will.
However, sometimes site owners need to implement custom functionalities that aren't available as a plugin. So, in that case, what should you do?
Well, there's a right and wrong way to do this. In this article, we will explain the different options you have and the best practices to add custom functionalities to a WordPress site.
Adding or extending functionality in a WordPress site
There are two main methods to add custom functionalities to a WordPress site. The first one is installing a plugin, and the other one is developing the functionality inside the WordPress theme.
The first technique is the most common way of incorporating new features into your site, and it's broadly considered a best practice in web development.
A Plugin is a package of code designed to extend the functionality of WordPress without having to edit WordPress core code (which should never be done)
Plugins can be installed, activated, deactivated, or deleted via the WordPress dashboard without affecting the website's other functions. Using plugins gives developers a lot of flexibility when testing, deploying or debugging code.
Themes, on the other hand, determine the design of a site. They dictate the way a web page looks and feels and how content is displayed.
Themes also define which fonts and colors the site has and where the sections, menus, and widgets are positioned, among other style-related things.
Some developers tend to add functionalities to the website into the theme's code using a child theme because it's a practical and straightforward way to extend WordPress functionality.
However, issues can arise when the child theme is used to implement functionality logic to a website.
That's why, before committing to any change, first you need to ask yourself:
Do I really need to customize my WordPress theme?
If the changes you want to implement on your site are focused on functionality rather than design, installing a plugin is the best way to proceed. It could be a plugin you develop, one you download from the plugin directory, or one you buy.
Most common examples of what you would use a plugin for are adding widgets, registering custom post types and taxonomies, creating custom fields, or adding features like an online store or SEO enhancements.
The right way to add custom functionalities to a WordPress site
When you add custom functionality by editing a theme's code, that feature may perform well initially, but it is likely to stop working when you update your site.
The correct way of adding functionalities that will last beyond your next update or theme switch is the following:
Using the Code Snippets Plugin
The Code Snippets plugin provides a graphical interface for adding code snippets to a WordPress site.
It's a clean and simple way to run PHP code snippets on your website, and it eliminates the need to add custom snippets to your theme's functions.php file.
Code snippets help you easily extend the functionality of a WordPress website. They are like a mini-plugin with less load on your site.
You can add as many snippets as you want, enable and disable them fairly simply, and even export them to then import them into other WordPress sites.
Building a custom plugin
You might find this idea daunting, but actually, it is not that difficult to build a custom plugin.
A WordPress plugin is essentially a PHP file with a WordPress plugin header comment. If you decide to create one yourself, we recommend you to create a directory to hold your plugin so that all the files are neatly organized in one place.
To get started creating a new plugin, you should follow these basic steps:
- Navigate to the WordPress installation's wp-content directory.
- Open the plugins directory.
- Create a new directory and name it after the plugin (e.g., plugin-name).
- Open the new plugin's directory.
- Create a new PHP file (it's also good to name this file after your plugin, e.g., plugin-name.php).
To know how to move forward from here, we recommend reading the WordPress Plugin Handbook.
How not to add functions to your WordPress site
The two most common bad practices when adding functions are editing an existing plugin or editing the functions.php file.
Don't edit plugin files
If your snippet changes a plugin's functionality, it may seem worthwhile to add the new code directly to the plugin.
However, when you update the plugin, the files you have changed will be overwritten, and your code will disappear.
The functions.php file is not for general-purpose custom code.
Each functions.php file belongs to a particular theme. If the code you want to add is theme-specific, then functions.php is a good place to put it.
But, when you switch themes, the new theme will not have the custom code. That’s why you should avoid putting general-purpose code in the functions.php file.
Why you should keep functionalities away from your theme
Keeping your website's functionalities decoupled from your theme is broadly considered a best practice in WordPress development.
Some of the main advantages of this include:
- Decoupling functionalities from the theme makes it easier to add new features and give maintenance to current ones.
- It's also easier to change the pages' look and feel without affecting the website's functionality.
- Switching themes is faster and more straightforward without depending on features implemented.
- It's easier to maintain the theme code and the functionality code because they are separate entities.
- Your website is more secure because you're able to update the theme as fast as the provider rolls out the updates. You also avoid the hassle of losing or having to update the functionality code.
If your theme is handling functionalities that are not design-related, the best is to look for a path of decoupling that functionality from the theme and move it into a plugin.
Beware that this can be a costly action to take. However, its benefits will be apparent over time. On the other hand, if you follow the best practices in WordPress development from the beginning, you won't have to deal with most of these expenses.
It's worth mentioning that decoupling might not always be worth it if the features are not complex enough or if they're not depending on the parent theme's core.
In those cases, it's relatively safe to leave them on the child theme. However, decoupling will be necessary if you want to switch to a different theme.
Things to consider if you have to decouple code from your theme
There are some things you have to consider when decoupling custom functionalities from your WordPress theme.
- You'll have to invest time to properly investigate all the functionalities that need to be decoupled. To do this, you need to go through the website's code and build a list including all the functionalities to decouple. Developers in charge of maintaining the website can help you make this list.
- Features that you miss to add to the list may become obsolete. Again, people who are more familiarized with these features will be of great help to adding them to the list when performing tests on staging.
- There will be a new curve of relative stability on these functionalities, new bugs will arise, and new fixes will need to be implemented, which will pose costs.
Decoupling functionalities from the theme might require a significant initial investment in terms of hours and resources. But however costly, it is way better to decouple any functionality coded directly on your WordPress theme.
Maintenance and development costs will be reduced over time, and you'll have more freedom to customize and incorporate new features to your site in the future.
Adding custom functionalities to your WordPress site shouldn't be hard at all. Most of the time, there will be a plugin that does the job. But, even if there isn't one around, you have several ways to make it work.
Following best practices in WordPress development is critical to ensure your website performs well on all devices, stays always updated, and your site's security isn't compromised in any way.
At Bixlabs, we specialize in providing web & mobile development services for startups worldwide. We see our clients as our partners. We strive to build lasting relationships where we can work hand-in-hand with our clients to create quality, successful products.
If you're looking for an experienced, reliable partner that helps you build or improve your WordPress site, give us a call!
This article was written by Ricardo Vargas, A.S. in Information Systems and Bixlabs' Wordpress expert; and by Guillermo Vidal, Journalist and Bixlabs' Content Creator.