Creating your first WordPress plugin allows you to easily add code snippets to your WordPress website. They will be completely independent of theme updates, and you can even install a new WordPress theme without worry. Also, you’ll no longer need to restore the functions.php file from your theme files, or fire up the Theme Editor and paste lines upon lines of code. This plugin can be used to add bonus functionality as well, including post types, shortcodes, or taxonomies. With that said, let’s demonstrate how to create a site-specific WordPress plugin.
1. Create a site-specific WordPress plugin folder and file
To create your first WordPress plugin, you must first make a new folder on your desktop. Name it however you want, for example, use your website name or abbreviation and add “-plugin”, resulting in wpthinker-plugin. Open the folder, add a new document, and give it the same name with a .php extension. In our case, that’s wpthinker-plugin.php. Right-click on it, and open it in a text editor. By default, you can use TextEdit on Mac and Notepad on Windows.
2. Add and modify site-specific WordPress plugin code
A crucial part of learning to a create site-specific WordPress plugin is making sure it’s in the correct format. Otherwise, it won’t be recognized by WordPress as a plugin. So, add this code to wpthinker-plugin.php:
<?php
/*
Plugin Name: Site-Specific Plugin for mywebsite.com
Description: Adding code snippets for mywebsite.com
*/
/* Add code snippets below this line */
Your code snippet goes here
/* Stop adding code snippets below this line */
?>
Replace “mywebsite.com” with the domain name of your website, and save changes.
3. Archive the folder
Now that you learned to build your first WordPress plugin, it’s time to prepare it for uploading. To be suitable, it must be in the form of a .zip archive. So, either utilize a specific third-party ZIP software or use the built-in utility like this:
- Windows. Right-click on the folder, then select Sent to > Compressed (zipped) folder.
- Mac. Right-click on the folder, then click on Compress “wpthinker-plugin”.
4. Upload the site-specific WordPress plugin
There are 2 ways to upload the site-specific WordPress plugin:
1. Via Admin Section
Access the WordPress Admin Section and then:
- In the main sidebar on the left, click on Plugins, then Add New.
- When the page opens, click on the Upload Plugin button in the upper left corner.
- Click on Choose File and select the wpthinker-plugin.zip file on the desktop.
- The plugin will upload, unpack, and install the WordPress plugin for you.
2. Via FTP
We already taught you how to access your WordPress website via FTP. What’s more, we demonstrated how to install a WordPress plugin via FTP. Make sure to drag and drop the wpthinker-plugin.zip file in step 6.
5. Activate the site-specific WordPress plugin
After a successful upload via WordPress Admin Dashboard, you’ll get an Activate Plugin button. If you skipped it, use steps 9 through 12 in the FTP guide above. With that done, you mastered how to create a WordPress plugin and can start using it immediately.
6. Can I edit my site-specific WordPress plugin afterward?
Yes, you can change the site-specific WordPress plugin without reuploading. Here’s what to do:
1. FTP
Access your website via FTP, then go to “public_html/wp-content/plugins/wpthinker-plugin”, and right-click on the wpthinker-plugin.php file. Select View/Edit, make necessary alterations, and save changes.
2. Admin Section
After logging in to the WordPress Admin Dashboard, do this:
- Click on Plugins in the sidebar on the left.
- Select Editor.
- Click on I understand when prompted with a warning message.
- In the top right corner, under Select plugin to edit, select yours from the drop-down list.
- Click on Select.
- Make necessary changes, then click on the Update File button at the bottom.
Install a plugin to create new plugins (Alternative 1)
We know how this sounds. But, if you plan to add tens or hundreds of code snippets, the process becomes time-consuming. Luckily, other WordPress developers realized this too. One example includes Samuel Wood (Otto), creator of a WordPress plugin named Pluginception. After installation, you get a Create a new plugin option under Plugins. Paste your code, save changes, activate your new plugin, and you’re done.
Install a dedicated WordPress plugin (Alternative 2)
Learning how to build a WordPress plugin is great. But, for this particular use, there are third-party alternatives that are more beginner-friendly and easier to use. One example is a WordPress plugin Code Snippets by Code Snippets Pro. After installation, you’ll get a Snippets section in the WordPress Admin Section. When you access it, you’ll see a list of code snippets, alongside their names, descriptions, tags, and priority. What’s more, you can toggle them with an on/off switch instead of deleting lines of code.