Beginner SeriesPlugin Development

WordPress Plugin Development Tutorial for Beginners – Part 2

By December 15, 2018 No Comments

WordPress Plugin Development Tutorial for Beginners Part 2

Welcome! This is part 2 of the new plugin development tutorial for beginners series. I’m not 100% sure how many parts there will be to this series but enough to properly breakdown the plugin template so that it’s easy to understand and eventually use. In this post, I will be going over the plugin header content, disclaimer, class declarations and construct function.

Let’s dive in.

Below is the first part of the plugin that declares a variety of things from the Plugin name to plugin url, description, text domain and so on.

  Plugin Name: NS WordPress Plugin Template
  Plugin URI:
  Description: A simple, fully functional WordPress plugin template that does NOTHING except give you a huge head start on building a new quality plugin.
  Text Domain: ns-plugin-template
  Author: Never Settle
  Author URI:
  Version: 1.0.1
  Tested up to: 4.9.8
  License: GPLv2 or later

In this example the plugin name is NS WordPress Plugin Template, the plugin url “link” is, the description is “A simple, fully functional WordPress plugin template that does NOTHING except give you a huge head start on building a new quality plugin“, text domain is ns-plugin-template, author is Never Settle, Author url is, Plugin Version is 1.0.1, version testing is up to 4.9.8 and the plugin license is GPLv2 or later.

webinsights plugin development image

All of these elements are important when creating a custom plugin for the kinds of information that each piece represents. WordPress has standards and these all meet the needed information for a plugin to be created. Also. all you really need for a plugin to exist is this information and it will load in wordpress. There wouldn’t be anything there but it would be recognized as a plugin.

Plugin Disclaimer and License

Every plugin that is posted on the directory needs to have a disclaimer as well as a license in for how the code/plugin can be used. The typical disclaimer is as follows:

  Copyright 2014 Never Settle (email :
  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
  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.

WordPress Licensing

Like most software, WordPress is distributed under a license, which means there are certain things that you are legally permitted (and not permitted) to do with WordPress software and source code. WordPress is distributed under a license called the GNU General Public License, a very popular license in the open source industry. If that doesn’t ring a bell, read on.

About the GPL

The GNU General Public License, or GPL, is an open source license. Open source doesn’t just mean that you can view the source code — it has political and philosophical implications as well. Open source, or “Free Software”, means you are free to modify and redistribute the source code under certain conditions. Free doesn’t refer to the price, it refers to freedom. The difference between the two meanings of free is often characterized as “Free as in speech vs. free as in beer.” The GPL is free as in speech.

Why We Chose the GPL

The reasons for WordPress releasing under the GPL are both practical and idealistic. WordPress was born of the very freedom mentioned earlier. The predecessor to the WordPress project, b2/cafelog, was also an open source project.

ABSPATH and sidebar php inclusion

Next in the code there is a ABSPATH line of code and an action called require_once. Below is the code.

if ( ! defined( 'ABSPATH' ) ) {
  exit; // exit if accessed directly!


The first line of code is smart to have because it limits anyone from accessing the main php file directly. When the plugin fires the code, if someone tries to access that file directly, it will use the exit command and kick them out. This is good for security!!

The require_once action is fired off to make what ever is included in the action, that is only required once in the process. In this example, the plugin requires once the sidebar.php page. There are a few other ones including: include, include once, require and require one and this posting goes into a bit more detail on that.

Plugin Classes

The next thing we are looking at are classes in plugins. This plugin defines a class called NS_plugin which is filled with all of the variables below from path to the plugin dir, url to the plugin page on etc. This process is a way to pre-define specific content in your plugin and then instead of retyping the code everytime, simply call the class in one simple $variable. The code below is the simplified version of a existing class functions etc.

// TODO: rename this class
class NS_Plugin {
  var $path; 				// path to plugin dir
  var $wp_plugin_page; 	// url to plugin page on
  var $ns_plugin_page; 	// url to pro plugin page on
  var $ns_plugin_name; 	// friendly name of this plugin for re-use throughout
  var $ns_plugin_menu; 	// friendly menu title for re-use throughout
  var $ns_plugin_slug; 	// slug name of this plugin for re-use throughout
  var $ns_plugin_ref; 	// reference name of the plugin for re-use throughout

For example: For the var $ns_plugin_name; variable on line 7 above, the function that is defined in this class is in the function below and is the following code.

function __construct(){		
  $this->path = plugin_dir_path( __FILE__ );
  // TODO: update to actual
  $this->wp_plugin_page = "";
  // TODO: update to link builder generated URL or other public page or redirect
  $this->ns_plugin_page = "";
  // TODO: update this - used throughout plugin code and only have to update here
  $this->ns_plugin_name = "NS Plugin Template";
  // TODO: update this - used throughout plugin code and only have to update here
  $this->ns_plugin_menu = "NS Plugin Menu";
  // TODO: update this - used throughout plugin code and only have to update here
  $this->ns_plugin_slug = "ns-plugin-template";
  // TODO: update this - used throughout plugin code and only have to update here
  $this->ns_plugin_ref = "ns_plugin_template";

As you can see on line 8 of the code above, the code $this->ns_plugin_name = "NS Plugin Template"; this defines the variable of ns_plugin_name as “NS Plugin Template”. All the code is doing is stating that $this->variable is equal to “this term”.

Why Do this?

It allows you to have consistency with your plugin code so that you don’t make a mistake in typing out the Plugin Name every time you need to use it. All you need to do is use this code $this->ns_plugin_name and the php automatically knows that you are using the plugin name you referenced earlier.

The last piece of the main class NS_Plugin are a series of add_action hooks that set up a variety of elements from register_settings_fields and admin_notices to admin_assets and setup_plugin. The code below shows this.

add_action( 'plugins_loaded', array($this, 'setup_plugin') );
add_action( 'admin_notices', array($this,'admin_notices'), 11 );
add_action( 'network_admin_notices', array($this, 'admin_notices'), 11 );		
add_action( 'admin_init', array($this,'register_settings_fields') );		
add_action( 'admin_menu', array($this,'register_settings_page'), 20 );
add_action( 'admin_enqueue_scripts', array($this, 'admin_assets') );

Add_action and hook definitions:

add_action( 'plugins_loaded', array($this, 'setup_plugin') );

This hook is called once any activated plugins have been loaded. Is generally used for immediate filter setup, or plugin overrides. The plugins_loaded action hook fires early, and precedes the setup_theme, after_setup_theme, init and wp_loaded action hooks.

add_action( 'admin_notices', array($this,'admin_notices'), 11 );

I’m not exactly sure what this hook does.

add_action( 'network_admin_notices', array($this, 'admin_notices'), 11 );

network_admin_notices is a hook triggered to output notices when a user is in the Network admin section (for multi site installs).

add_action( 'admin_init', array($this,'register_settings_fields') );

Most of WP is loaded at this stage, and the user is authenticated. WP continues to load on the ‘init’ hook that follows (e.g. widgets), and many plugins instantiate themselves on it for all sorts of reasons (e.g. they need a user, a taxonomy, etc.). This hook fires the register_settings_fields hook

add_action( 'admin_menu', array($this,'register_settings_page'), 20 );

This action is used to add extra submenus and menu options to the admin panel’s menu structure. It runs after the basic admin panel menu structure is in place. This hook fires the register_settings_page.

add_action( 'admin_enqueue_scripts', array($this, 'admin_assets') );

admin_enqueue_scripts is the first action hooked into the admin scripts actions. It provides a single parameter, the $hook_suffix for the current admin page. Despite the name, it is used for enqueuing both scripts and styles.

Final Thoughts

That is it for this part. Next comes the functions for each of the add_action hooks we just created. As you can see this template plugin is relatively simple but coding in PHP can be complex and detailed. Creating custom wordpress plugins takes time and focus but once you know the main elements you need, the options are endless.

Good luck and stay tuned for the next post in this WordPress Plugin Development Tutorial for Beginners Series. If you missed the first part in this series check it out here. WordPress Plugin Development Tutorial for Beginners – Part 1

Leave a Reply