WordPress is the most popular content management system in the world. Its popularity is mainly because of the ease of use and availability of thousands of plugins. These plugins help us to add almost any functionality to our WordPress system in a few simple steps. We can also do modifications to functions.php file to add our custom functionality to our website. One of the major drawbacks of this method is its dependency on the theme. The functions.php file is within the theme’s folder and so this file also gets overwritten when we switch themes. The best way to resolve this issue is to create a site-specific WordPress plugin. Let us first see some details about site-specific WordPress plugin before proceeding with the steps to create it.
What is a site-specific plugin?
A site-specific plugin helps us to add custom code snippets to our WordPress system that are independent of themes. This helps us to customize our WordPress installation without the fear of losing our custom code changes when we switch themes. In short, we are creating a plugin that has our custom code changes and later install it on our WordPress website just like normal plugins. The idea of creating a plugin might seem a bit intimidating to non-developers. But in reality, this process is not at all complicated and can be achieved in a few simple steps.
How to create a site-specific plugin?
Step1: Create a folder on our system and name it something similar to yoursitename-custom-plugin. For me, the folder name would be blogtriangle-custom-plugin.
Step2: Now open a text editor tool like Notepad or Notepad++. Copy the below lines of code to this file.
Plugin Name: Custom plugin for blogtriangle
Description: Custom Code to be added here
/* Add Your Functions Below this Line */
/* Stop Adding Functions Below this Line. Do not add functions below this line */
Replace blogtriangle with your site name in the above content. Save this file as yoursitename-custom-plugin.php within our already created folder by selecting the All files option from the Save as type drop-down menu.
Step3: Right-click on the folder and select Send to Compressed (zipped) folder option to create a zip file of this folder. If you do not have this option then install WinZip or 7-Zip and then create a zip file of this folder.
Step4: Let us now go to our WordPress admin dashboard and select the Add New plugin option.
In the new screen select the option to Upload Plugin.
Now options to upload a zip file will appear. Just select the zip file that we have created before and click the Install Now button.
Once the plugin is successfully uploaded the below screen will appear.
For now, let us not activate the plugin as we have not added any code snippet within our php file.
Step5: Let us now go to the Plugin Editor section to view our code.
We will get a warning message as shown below. Click the option I understand to proceed.
Select our custom plugin name from the drop-down menu and click the Select button to view our plugin code.
We can now add a meaningful code within the specified area for demo purposes. I have added the line
to the file. This code snippet is for deactivating XML-RPC on our WordPress website.
Now click the Update File button to save our changes.
Step6: Now go back to our plugins section and find our custom plugin.
Click on the Activate button to activate our plugin.
We can test if our plugin is working by checking the XML-RPC status of our site. For this go to the XML-RPC Validator website. We can just enter our website URL and check if XML-RPC is disabled or not. Do this before and after activating our custom WordPress plugin.
Thus we have successfully created a site-specific plugin and activated it on our WordPress installation in a few simple steps. Similarly, we could add any code snippet to this plugin. These code changes remain unaffected even if we switch themes. We can deactivate the plugin if we want to remove the code snippets.