WordPress offers a lot of features right out of the box. If you want to create a blog, you can start using WordPress without the need to add any additional plugin.
When you want some additional functionality not provided by the core WordPress, you can find and install a plugin from the WordPress plugin repository.
WordPress plugins repository contains thousands of plugins and in most cases, you will find the plugin for your required functionality.
But there are cases when you do not find the perfect plugin. There may be plugins, but they are so bloated that you do not want to use them. Or, you need a very small functionality and installing a full-blown plugin with a lot of unnecessary features may seem illogical.
In such cases, it is always better to write your own code for adding that functionality. But where to add that code?
Where to add the code
If you want to modify a core WordPress feature, you may be tempted to edit the WordPress core files directly. But this is not a sustainable practice as whenever WordPress will be updated, your changes would be overwritten.
If you want to change the functionality of your current theme, you edit the
functions.php file inside your theme. But again, if the theme is updated, you will lose the changes.
In short, you must NOT edit any core WordPress files, themes or plugins. Rather, you need to add code to override or extend that functionality based on the APIs provided by WordPress.
There are two recommended ways to add custom code to WordPress-
- Create a child theme of your current theme and add code to its
- Create a custom plugin and add it to your website
Theme vs Plugin
WordPress has very nicely separated the design and development functionality. Themes are meant for the front-end design of the website and plugins are meant for adding functionality to the website.
It is important to strictly follow this separation logic to ensure a clean and easy to maintain code.
WordPress provides the functionality to add a child theme to override the current theme's files. You can override templates and add code to
But you must not add any code to a theme that is not related to front-end design. All such code should reside in plugin only. Plugins are the right place for all your non-design functionality.
I have seen a lot of WordPress tutorials on various popular websites that encourage users to add custom code to their child theme's
functions.php file. But this approach is wrong.
If you add your custom code to WordPress child theme, you violate the code separation logic, thus messing up your website for the future.
Creating a small single-page plugin and adding it to WordPress is easier compared to adding a child theme. Plugins are easy to manage and you can easily create a directory structure for your code if you are creating a complex plugin with multiple functions.
Is it safe to add custom code to my WordPress blog
Security is a very important concern when writing code. If you are not a good programmer and somehow you leave any vulnerability in your code, it can be a huge issue for your website. To make sure that this does not happen, you should follow proper coding standards and best practices.
If it is a small function, it is very easy to ensure that it is free from vulnerabilities. But in case you are planning for a complex plugin, make sure that you properly review your code and test the code for all possible scenarios.
If you are a decent programmer, you can write code which is secure and clean. Though security is important, fear of security must not discourage you from creating your own plugin. It actually enhances your skill and gradually you become good at it.
Just carefully write your code, review it properly and test it thoroughly. You can refer to security best practices for coding, you can review the code of plugins offering similar functionality or if needed, you can take an online course to improve your programming skills.
It is not reinventing the wheel
A lot of people tell you not to write your plugins if someone has already created a plugin with your required functionality as you will be reinventing the wheel.
This logic does not work efficiently in the coding world. Yes, creating an entire CMS like WordPress to host your blog is surely reinventing the wheel. But, adding some code to add functionality may not be the same.
If you are writing code to avoid installing a badly coded or bloated plugin to your WordPress blog, it is a good practice.
If you write a good plugin which works nicely for your WordPress blog and you think that it will be beneficial for a lot of WordPress users, you may decide to release the code publicly by submitting it to WordPress plugin repository.
This way, you can contribute back to the WordPress community and at the same time, it will help you improve your coding skills.
WordPress may be full of a lot of functionality, there may still be many things where it may fall short of your expectations. Though a lot of plugins are available in the WordPress plugin repository, you may not find the plugin which suits your requirements. This calls for writing your custom code and add it to your WordPress blog.
Writing custom code for WordPress may be easy, but you should carefully add your code at the correct place to keep it clean and easily maintainable.
The best way to add your custom code to WordPress is to create a plugin and add it to your WordPress blog.
Do make sure that you follow proper coding standards and best practices. Also, do proper code review and testing to ensure that your code is safe.