How to Build a Simple WordPress Plugin

image
image
image
image
image
image
How to Build a Simple WordPress Plugin

How to Build a Simple WordPress Plugin

WordPress is one of the most user-friendly CMS (Content Management Systems) today. It is an ideal way for novice computer users to be able to maintain and update websites. With a little bit of training, you can quickly be brought up to speed on how to use WordPress as a CMS.

We love WordPress Plugins. WordPress Plugins are handy tools that let you add all kinds of features to your website in seconds.

However, sometimes you can’t find a plugin that meets your needs, or you may simply want to try your hand at developing your own solution.

In that case, then you’re in luck. It’s actually considerably more comfortable to create a WordPress Plugin than you might expect.

In this post, I will explore all the essential WordPress Plugin development steps. I will explain how plugins work and discuss how they fit into the WordPress CMS. 

Then we’ll discuss what you need to know, before creating your very first WordPress Plugin. There’s a lot to cover, so let’s get started!

Introduction to Hooks, Actions, and Filters

The most challenging part of getting started with plugin development is to learn all the related terminology. For that reason, I am going to explain some of the most important and commonly-used terms.

Hooks are connection points provided by WordPress where you can attach your plugin to the WordPress core code.

Basically, hooks determine when and where the plugin will be used on a website.

There are two kinds of hooks:

Actions

An Action is a custom PHP function defined in your plugin (or theme) and hooked, i.e., set to respond to some of these events.

The basic steps to make this happen (described in more detail below) are:

  • Create a PHP function that should execute when a specific WordPress event occurs, in your plugin file.
  • Hook this function to the event by using the add_action() function.
  • Put your PHP function in a plugin file, and activate it.

Example:

function owi_display_hello_world() {

    echo 'Hello World';

}

add_action( 'my_action', 'owi_display_hello_world');

Filters

These are used to alter the functionality of actions.

Example:
We can change the post content via function the_content() to ‘Hello Welcome’.

function owi_display_hello_world() {

    echo 'Hello World';

}

add_filter( 'the_content', 'owi_display_hello_world' );

There’s a lot more to about hooks. This will give you a rough idea of how the two differ.

How to create your first Wordpress plugin

First create a new folder inside this directory. Do so now, and give it any unique name you’d like.

I will call my folder owi-hello-world 


That folder is where everything related to your plugin will ‘live’ on your website.

Since this plugin is going to be very simple, it only needs to contain a single file, which you’ll create now. This will be a PHP file, which will contain the plugin’s code.

I will call my file owi-hello-world.php


The file is empty now so paste the following text into it:

/**
* Plugin Name: OWI Hello World
* Plugin URI: http://www.owi.ie
* Description: This OWI Hello World Plugin
* Version: 1.0.0
* Author: OWI Web Development
* Author URI: http://www.owi.ie
* Text Domain: owi-hello-world
* Contributors: OWI Web Development
**/

Here below fields are absolutely required and should be unique to your plugin:

  • Plugin Name
  • Plugin URI
  • Description
  • Version
  • Author
  • Author URI
  • Text Domain
  • Contributors

Feel free to change any information, when that is done, you can actually see the plugin in your WordPress website’s admin dashboard. Log in now and take a look in your plugin library.


Add submenu and “Hello World” landing page on your plugin

/**
* Plugin Name: OWI Hello World
* Plugin URI: http://www.owi.ie
* Description: This OWI Hello World Plugin
* Version: 1.0.0
* Author: OWI Web Development
* Author URI: http://www.owi.ie
* Text Domain: owi-hello-world
* Contributors: OWI Web Development
**/

/**
*
* Adding Submenu under Settings Tab and Display hello world
*
**/

function owi_add_menu() {

    add_submenu_page ( "options-general.php", "OWI Hello World", "OWI Hello World", "manage_options", "owi-hello-world", "owi_hello_world_page" );

}

add_action ( "admin_menu", "owi_add_menu" );

function owi_hello_world_page() {

      echo '<h1>OWI - Hello World Plugin</h1>';

}




That’s it. Begin Writing Your First WordPress plugin.

Thanks for reading and give me comments for what you disagree. No comment? how about a recommendation, this is how you can do it.

Leave a Reply

Your email address will not be published. Required fields are marked *