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. You can quickly learn how to use WordPress as a CMS with some training.

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 want to try to develop your solution.

In that case, then you’re in luck. Creating a WordPress Plugin is considerably more comfortable than you might expect.

This post will explore all the essential steps for developing WordPress plugins. 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 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. Therefore, I will 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.

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 to 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 

How to build a simple wordpress plugin
How to build a simple wordpress plugin

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

Since this plugin will 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

How to build a simple wordpress plugin
How to build a simple wordpress plugin

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
**/

The below fields are 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. You can see the plugin on your WordPress website’s admin dashboard when that is done. Log in now and take a look at your plugin library.

How to build a simple wordpress plugin
How to build a simple wordpress plugin


Add a submenu and a “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>';

}
How to build a simple wordpress plugin
How to build a simple wordpress plugin

 

How to build a simple wordpress plugin
How to build a simple wordpress plugin

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 make it.

Ultimate UX Design Guide
29 May 2019
Chrome Extensions for Web Developers
08 Jul 2019

Leave a Reply

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

Insights & Inspiration

Keep up to date or learn a new skill with our website design and development content.