Skip to main content

How to Develop Plugin in WordPress?

WordPress is a award winning CMS that changed the trends of blogging. It provides easy way to create and manage your blog. you can easily modify, customize and enhance functionality of your WordPress blog. You don’t need to change the core functionality of your WordPress blog. It provides an easy way to add functionality with WordPress plugins. A WordPress plugin is a program or can say a set of one or more functions which is written in the PHP scripting language which adds a specific set of features to your WordPress website.

WordPress has a very long list of available plugin. you can search plugins that meets your requirement. If not, this article will help you to develop your own WordPress plugins. Here we will going over the basic steps required to create a simple WordPress plug-in.

 



1. WordPress File Structure

Here you will learn the basic folder structure of WordPress. you will find a plugin directory into wp-content directory where all of your individual plugins are stored as single files or properly named sub-directories for your plugins. For smaller plugins which only have single .php file, can be placed directly into plugin/ directory. However when you have more complicated functionality then it is useful to create a sub directory named after your plugin. You can also keep your JavaScript,CSS, and html include into your individual plugin.

wordpress plugin directory
You can also add read.txt if you are planning to offering your plugin for download. This file will contain your plugin name and what the plugin does.

2. Write Simple Plugin File

Now we will create with a simple php file. The name of file will be named with plugins specific name. So here I will have a php file with base code named as phpzag.php.

The first lines of your plug-in must be comment information for the parsing engine. This is extremely important as WordPress will be unable to process your file without it.  these line will show basic information of your plugin.
<?php
/*
Plugin Name: Plugin Name here
Plugin URI: http://www.pluginurlhere.com/
Version: Current Version
Author: Name please
Description: What does your plugin do and what features does it offer…
*/
?>

The Plugin Name is what will show up in your Admin back-end panel when you go to activate. Same with the URI which will be placed in the details pane inside the plug-ins panel. Although it’s not required to include a version or description it does make your plugin look much more professional.

3. WordPress Naming Conventions And Best Practices

WordPress doesn’t need any specific code structure into your plugin. if you are well versed with php opps concept, then its best to handle using class and functions. if are unfamiliar with the advanced OOP functionality of PHP then its best to just write your code in simple functions. Also its best to follow basic code guides set up by WordPress. Since there are so many internal functions already defined you can avoid duplicates by prefixing a label to all your variables and function names.
<?php
define(“Variable”, “Varriable label value”);
function content_example_function( $limit ) {
// some code goes here
}
?>

The above functions is just a sample function which give you some insight into how your variable names and function should be written.

4. Diving Into Hooks, Actions and Filters

Hooks are provided by WordPress which allows your plugin to hook into the core functionality of your blog. The hooks are divided into two parts that’s Actions and Filters.

Actions are occurred during specific events. Below are list of few important actions of WordPress.
add_action(‘save_post’, ‘notify’);

In above code have two variables, the first holds the name of our hook we’re targeting. In this case save_post which means whenever a new post is saved we’re going to call our function defined in the second parameter. You could obviously update notify to be whatever function name you’d want to run, however this isn’t required for our current example plug-in.

Filters are occurred to modify text of various types before adding it to the database or sending it to the browser screen. your plugin can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API. For example, you may create a filter to change $the_content which is a variable set by WordPress containing the entire post content of a WordPress article. For our plug-in we will be taking $the_content and shortening the length of characters into an excerpt.

Actually Filters are handy when you are writing plug-ins to customize the looks and feel of your blog. These are especially popular when writing sidebar widgets or smaller functions to change how a post should be displayed. Below is a sample line of code showing how to apply a filter.
add_filter(‘wp_title’, ‘func’);

The add_filter function is native to WordPress and used to add a new filter to a variable found within page content.

Complete Plugin Logic

This is a complete logic of our plugin. it can be placed inside your theme’s function.php file. the purpose of this code is to limit the range of post content.
<?php
function phpzag_excerpts_content( $limit ) {
$content = explode( ‘ ‘, get_the_content(), $limit );
if ( count( $content ) >= $limit ) {
array_pop( $content );
$content = implode(” “,$content).‘…’;
} else {
$content = implode(” “,$content);
}
$content = preg_replace(‘/\[.+\]/’,”, $content);
$content = apply_filters(‘the_content’, $content);
return $content;
}
?>

The above code just require general understanding of how your functions should be written. you will notice that we are using a call to apply_filters which is another WordPress-specific function.

The function above is named phpzag_excerpts_content(). This only requires 1 parameter named $limit. This could also be shortened to $limit which should store an integer specifying how many characters to limit your excerpt to. The content is used on full post pages and also static pages like about us, contact etc.
<?php
echo phpzag_excerpts_content(60);
// display page content limited at 60 chars
?>

Installing And Running The Plugin

This is just a general description how to develop WordPress plugin. if you want to run the code above. simply download the plugin file and rename it to .php file. Then upload this file to your blog’s /wp-content/plugins directory.

after uploading plugin file, go the the admin panel and browse your plugin and make it active. Once you activate nothing new will happen, until we manually add in our function call. To do this simply navigate Appearance -> Editor and look for single.php. This file contains all the HTML/CSS for your basic article post page. Scroll down until you find the_content() and replace with the example code above. this will limit all your article pages to 60 characters. you can also add this function to other files.

In this article I have presents just a  general idea of how you can start working with WordPress plugin development. AS you gone through this small article, you can understand that plugin has very vast and deep internal functionality. But you can easily command over by  start from here or also through WordPress official Documention, development community and forums.

Leave a Reply

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

Shares

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!