Out of the box, WordPress is a pretty flexed CMS platform and can do many things. However, often you’ll find that it is not enough.
Plugins are a great way to extend the functionality of WordPress and get more out of it.
Exactly like WordPress, these plugins are well built but they are not perfect. Code snippets can be hooked to the plugin’s PHP files and make it better by performing the desired actions for us.
Let’s take us for example, we use the Easy Digital Downloads plugin to sell the plugins and themes. EDD has many extensions and add-ons, but we still needed a custom code to redirect the empty cart page to another page.
During your WordPress journey, you’ll run into tutorials that require you to use code snippets and paste them into the
functions.php file or in a site-specific plugin.
These custom codes or snippets have many uses and mostly made to improve or expand your WordPress website.
We can paste code snippets into the
functions.php file and it will execute perfectly. However, It is not the best practice and should be avoided in some cases.
In this article, we will explain in detail how to create a site-specific WordPress plugin and when to use or not to use the
functions.php file for code snippets and other functions.
When To Use
functions.php in a child theme is a good way to execute WordPress functions in these cases:
- To add another stylesheet for a theme or specific page.
- Filtering hooks for a specific theme.
- Action hooks for a specific theme.
- Add a translation file or font for that theme.
You see, all functions related to a specific theme can be used in the
functions.php file of the child theme.
We wrote an article on how to create a child theme in WordPress if you don’t already have one.
When Not To Use
Pretty much in any other situation not mentioned above. Here is a rule of thumb, All functions that are not related to a specific theme and can be carried over from one theme to another should be created as a stand-alone plugin. Here are only some of the many scenarios:
- When creating a shortcode or a widget.
- Functions related to another plugin (like the one we talked about earlier).
- Implementing codes (such as chatbox or Google Analytics tracking code).
Also, it’s much easier to debug WordPress errors when each function is a stand-alone plugin. If for some reason, WordPress failed to load, you can deactivate one plugin after another and detect which plugin caused the issue.
So, now that we know when to use the
functions.php file and when to use a custom plugin, it is a good time to find out how to create a custom WordPress plugin.
We can create custom WordPress plugins in two ways. Create the plugin by ourselves from scratch, or use another plugin to create a plugin.
How To Create a Custom WordPress Plugin
The title above may sound scary but actually, it is not. It is a really short and easy process.
Step #1: Open a text editor and create a PHP file.
Fill the empty document with:
<?php /* Plugin Name: Your Custom Code Name */ /* Add Your Code Snippets Below This Line. */ ?>
In our case, we will paste the EDD function to redirect the empty checkout page to another page. It will look like that:
Save the document as a PHP file. If you can’t find the option to save it as PHP, just save it as a regular TXT file and change the extension of the file afterward inside its folder.
Step #2: Compress the PHP file into a ZIP file.
Right-click on your freshly made PHP file and then click on ‘Compress YourCustomPlugin.php’. That will create a ZIP file that contains your plugin inside of it. We can move forward to the next and final step.
Step #3: Upload and activate the new WordPress custom plugin.
This step is an easy one and you probably did it a few times before.
All you have left to do is log-in to your WordPress dashboard and navigate into Plugins->Add New.
Click on ‘Upload Plugin’ and then ‘Choose file’. Choose the new ZIP file that we just created and click on ‘Install’ and then on ‘Activate’.
That’s it, your new plugin is now up and running. You can now visit your website and make sure it’s working properly.
You can now see the new plugin on the plugins page with all the other plugins that you have on your website. Feel free to activate, deactivate or delete it if necessary.
How To Create a WordPress Plugin With Another Plugin
As promised, creating a WordPress plugin from scratch as I showed you above was a quick and easy process.
However, we can make it even quicker and easier (I know. Shocking, right!?).
When we use another plugin to help us create our plugin, we can save time and skip both step one and step two above.
To accomplish that, we will use a plugin called Pluginception.
Step #1: Install the Pluginception plugin.
From inside your WordPress dashboard, navigate to Plugins -> Add New and search for Pluginception. Install and activate it.
Step #2: Create the custom plugin
After activating the plugin, you’ll see a new option to ‘Create a new plugin’ under the Plugins tab. Click on it.
On this screen, you’ll need to name the plugin (it’s the only required field). Click on the ‘Create a blank plugin and activate it’ button.
On the next screen, you’ll need to paste your custom function click on ‘Update file’.
That’s it, your new custom WordPress plugin is up and running. You can visit your website now and make sure everything is working as normal.
You can now see the new plugin on the Plugins page with all the other plugins that you have on your website. Feel free to activate, deactivate or delete it if necessary.
We hope that this article helped you to get a better understanding of the reasons and the benefits of using and creating a site-specific, stand-alone plugin overusing the
Feel free to share with us about your code snippets or how was your process of creating a custom WordPress plugin in the comment section down below.