Today we will learn how to create a site specific plugin for our WordPress site. Before we get started on creating the file, lets briefly talk about why these plugins are helpful.
In the past, if you wanted to add a code snippet to your site then you might use your theme’s functions.php file which is usually in this location:
One problem with this approach is that it does not account for theme updates which means there is a risk of losing your customizations if you update your theme. This is especially true if your theme has a convenient one-click update feature from within the WordPress admin. A site specific plugin is a better choice since your customizations will be in organized in one location. Additionally, if you add your code through the WordPress Plugin Editor, then WordPress will check your code to ensure that it won’t cause any fatal errors and take your site offline. Finally, some customizations require loading the code early in the request, so by the time the theme’s functions.php file loads it’s too late!
The Solution: Create your Site Specific Plugin
To get started, you will need to login to your WordPress root with an SFTP or FTP client. Cyberduck and FileZilla are free options that are available on multiple platforms. This will require an SFTP or FTP login which can be provided by your web host.
1) After logging in to your WordPress root, browse to this location:
2) Now create a new folder. In this example, we’ll create one called myexamplesite:
3) Next, open the new folder that you just created.
4) We now need to create a file. In this example, we will create a file called myexamplesite-customizations.php:
5) Then, copy the sample code below:
<?php /* Plugin Name: Site plugin for myexamplesite.com Description: Site specific code for myexamplesite.com */ /* Begin Adding Functions Below This Line; Do not include an opening PHP tag as this sample code already includes one! */ /* Stop Adding Functions */
6) Return to your SFTP or FTP client and paste the sample code above into the file. You can change “myexamplesite.com” to something else such as your own site URL. Be sure to save changes to the file.
6b) You may need to update the file permissions for the file. Check with your web host for the required file permissions.
7) Now login to your WordPress admin and go to Plugins –> Editor. Select your new plugin from the dropdown in the top right corner:
You can now add your code snippets to this file and save changes by clicking on Update File.
Remember, by using the WordPress Plugins Editor, WordPress will check your code to ensure that it won’t crash your site and now your customizations will be protected from theme updates.