Select Page

Create a WordPress Plugin Tutorial (With Boilerplate)

by | WordPress Plugin Development

WordPress plugins are extremely useful – whether you are building a quick custom plugin to speed up internal operations, or if you plan to publicly release a useful plugin to the world, this guide is built to help you create a WordPress plugin while answering the most common questions I had when I first started building them

6 Steps to Beginner WordPress Plugin Creation

Quick-Links to Contents:

  1. Setting up a Dev Environment (Optional)
  2. Creating the Basic Plugin Files
  3. Check and Activate the Plugin
  4. Enable Logging for Debug Purposes
  5. Give Your Plugin Some Functionality
  6. Submit to the WordPress Public Directory (Optional)

1. Setting up a Dev Environment (Optional)

A local development environment isn’t a requirement, but it does help ensure you don’t accidentally get a 500 error due to a missing semicolon. I honestly like using Local by Flywheel since it’s easier than manually installing WordPress on my MacBook using something like MAMP – it’s free if you want to check it out.

Whether you have Local or some other platform installed, create a fresh new WordPress site that we won’t feel bad about destroying (if we have to).

2. Creating the Basic Plugin Files

To begin work on your new plugin, browse through the WordPress source files to /wp-content/plugins/. Create a new folder there named after your new plugin, such as “my-amazing-plugin”. Inside that folder, create a file with the same name, such as “my-amazing-plugin.php”. Open that file and paste in this markup and license text boilerplate:

<?php

/* /wp-content/plugins/my-amazing-plugin/my-amazing-plugin.php */

/*
Plugin Name: My Amazing Plugin
Description: Your Plugin Description.
Version: 1.0
Author: Your Name
Author URI: http://yourwebsite.com
License: GPLv2 or later
Text Domain: my-amazing-plugin

*/

/*

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Copyright 2019 Your Name or Company

*/

Then create a file called readme.txt and use this markup boilterplate text:

/wp-content/plugins/my-amazing-plugin/readme.txt
=== My Amazing Plugin ===
Contributors: yourwordpressusername
Tags: 
Requires at least: 4.0
Tested up to: 5.1
Stable tag: trunk
License: GPLv2 or later

== Description ==

Your description here

== Features ==
* List your features
* One by one


== Changelog ==

= 1.0 =

* Initial release.

3. Check and Activate the Plugin

In the admin dashboard for your WordPress development site, go to Plugins then Installed. You should now see your new plugin listed just like all the rest, but it will be inactive. Make sure to click ‘Activate’ otherwise you’ll go crazy like I did trying to figure out why your plugin functions aren’t taking hold.

4. Enable Logging for Debug Purposes

When you create a WordPress plugin, you will find that it is very helpful to check what data your variables are holding at certain times, especially when you start introducing API calls and user input. First you need to enable the WordPress debug log, which is the primary log file used by all WordPress functions. Add the below lines to the wp-config.php file located at the root of the website files (above the line that says /* That’s all, stop editing! Happy blogging. */):

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

Then, paste this function in your my-amazing-plugin.php file so you can write to the main WordPress debug log from your plugin:

// Source: https://www.elegantthemes.com/blog/tips-tricks/using-the-wordpress-debug-log
// Usage example: write_log('The variable value is: ' . $myVariable);
if ( ! function_exists('write_log')) {
  function write_log ( $log ) {
    if ( is_array( $log ) || is_object( $log ) ) {
      error_log( print_r( $log, true ) );
    } else {
      error_log( $log );
    }
  }
}

5. Give Your Plugin Some Functionality

This code will allow your plugin to reach out to a Ron Swanson quotes API, and then show the response inside the WordPress admin dashboard:

function myplugin_admin_notice__success() {
  $quote = myplugin_getQuote('https://ron-swanson-quotes.herokuapp.com/v2/quotes');
  ?>
  <div class="notice notice-success" style="display:flex;align-items:center;">
  <p style="display:inline-block;"><?php echo $quote ?> -Ron Swanson</p>
  </div>
  <?php
}
add_action( 'admin_notices', 'myplugin_admin_notice__success' );

function myplugin_getQuote($url) {
  $response = wp_remote_get( $url );
  $body = wp_remote_retrieve_body( $response );
  $body = substr($body, 1, -1);
  return $body;
}

6. Submit to the WordPress Public Directory (Optional)

Once you are happy with your plugin, simply compress its folder in /wp-content/plugins/ to a .zip file. Then, go to this WordPress page and click ‘Please Log In’ towards the bottom of the page. Login or create your WordPress developer account to then upload your plugin’s .zip file. Expect an email from WordPress saying that your plugin is queued to be reviewed.

The nice part about this process is an actual human on their team will email you back and let you know if there are cleaner WordPress functions you can use that you might not have known about, or they will simply tell you that you have a nice plugin and approve your upload. Once you are approved you are then allowed to upload your plugin files to WordPress’ public plugin repository using your preferred SVN client (similar to the idea of git version control, but more focused on publishing milestone versions of software than tracking tiny changes).

Let me know if there are any questions I left unanswered as you create your first WordPress plugin!