WordPress is a pretty flexed CMS platform and can do many things out of the box. 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 improved by performing the desired actions.
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 are mostly made to improve or expand your WordPress website.
We can paste code snippets into the
functions.php file and execute them flawlessly. However, It is not the best practice and should be avoided in some cases.
This article will explain 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 an excellent way to execute WordPress functions in these cases:
- To add another stylesheet for a theme or specific page.
- Filtering hooks for a particular 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.
When Not To Use
Pretty much in any other situation not mentioned above. Here is a rule of thumb: All functions unrelated to a specific theme and carried over from one theme 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 discussed 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 WordPress fails to load for some reason, 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 an excellent 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 straightforward 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, save it as a regular TXT file and change the file’s extension afterward inside its folder.
Step #2: Compress the PHP file into a ZIP file.
Right-click on your freshly made PHP file and click on ‘Compress YourCustomPlugin.php.’ That will create a ZIP file that contains your plugin inside it. We can move forward to the next and final step.
Step #3: Upload and activate the new WordPress custom plugin.
This step is easy, and you have probably done it a few times before.
All you have left is to 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 we just created and click on ‘Install’ and ‘Activate.’
That’s it. Your new plugin is now up and running. You can now visit your website and make sure it’s working correctly.
You can now see the new plugin on the plugins page with all the other plugins 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 more accessible (I know. Shocking, right!?).
When we use another plugin to help us create our plugin, we can save time and skip steps 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. Please 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.
You’ll need to name the plugin (the only required field). Click on the ‘Create a blank plugin and activate it’ button.
You’ll need to paste your custom function and click on ‘Update file on the next screen.’
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 expected.
You can now see the new plugin on the Plugins page with all the other plugins 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 your process of creating a custom WordPress plugin in the comment section down below.