Having your own WordPress snippets plugin allows you to easily add and manage custom code snippets on your WordPress website without having to add custom snippets to your theme’s functions.php
file. The plugin also handy if you do not have a child theme installed, and it can help you organise your code snippets and make them easier to find and use.
How To Create The Plugin
When naming a WordPress plugin, it’s important to choose a name that accurately describes what the plugin does and is easy to remember.
To get started, create a blank folder on your desktop and name it websitename-snippets, replacing websitename with your own website’s name or brand name e.g pimlicoflowershop-snippets.
Inside the new folder create a new PHP file and name it websitename-snippets.php
.
Open websitename-snippets.php
with a text editor such as Notepad or a more advanced code editor like Sublime Text or Visual Studio Code.
Now copy and paste the following plugin header comments inside your websitename-snippets.php file and save the file.
<?php
/**
* Plugin Name: Websitename Snippets
* Plugin URI: https://www.mywebsitename.com
* Description: My Websitename code snippets
* Version: 1.0.0
* Author: My Name
* Author URI: https://www.mywebsitename.com
* Text Domain: websitename-snippets
* Domain Path: /languages
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
// Include your custom code below here.
Next, you will want to change the plugin’s header comments with your own information. The most important fields are the following:
- Plugin Name: The name of your plugin, which will be displayed in the Plugins list in the WordPress Admin.
- Plugin URI: The home page of the plugin, this should be a unique URL, preferably on your own website.
- Description: A short description of the plugin, as displayed in the Plugins section in the WordPress Admin. Try to keep this description short.
- Author: The name of the plugin author (you).
- Author URI: The author’s website URL.
As an example, I will call my website The Pimlico Flower Shop, and I will customise the plugin to include my website’s information:
<?php
/**
* Plugin Name: The Pimlico Flower Shop
* Plugin URI: https://www.pimlicoflowershop.com
* Description: The Pimlico Flower Shop custom code snippets.
* Version: 1.0.0
* Author: Berto
* Author URI: https://www.pimlicoflowershop.com
* Text Domain: pimlicoflowershop-snippets
* Domain Path: /languages
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
// Include your custom code below here.
Save the file and you are done! You have just created your very own snippets plugin.
ABSPATH
But wait, you didn’t explain what this line of code on the plugin does?
ABSPATH is a PHP constant, which holds the Absolute path to the WordPress directory.
if ( ! defined( 'ABSPATH' ) ) exit;
The snippet above ensures that the plugin can only be accessed from within the WordPress dashboard and not directly from the web. It helps to prevent attackers from accessing sensitive information or executing malicious code on the website.
Uploading The Plugin
To upload the new plugin to your website, you can either compress it into a zip file and upload it directly from the WordPress plugin area, or use FTP to connect to your website and upload the folder and file to the /wp-content/plugins
directory.
Once uploaded you can activate the plugin from the WordPress plugin manager.
Going forward, you can start adding your own WordPress code snippets just below the line where it says // Include your custom code here.
Boilerplate Generators
Boilerplate plugins are intended to save developers time and effort by providing a starting point for building new plugins, rather than having to start from scratch each time. Boilerplate plugins typically follow WordPress coding standards and best practices and can be customised to suit the specific needs of the plugin being developed.
Two boilerplate generators that can help you get started with a blank canvas are Plugin Plate and the WordPress Plugin Boilerplate Generator. Plugin Plate allows for customisation and the ability to exclude unnecessary files, while the WordPress Plugin Boilerplate Generator is geared towards advanced users.