How To Create a WordPress Plugin from scratch?

advertisement

advertisement

WordPress plugins allow you to modify, customize and add new features to the WordPress platform. Plugins can be crafted to each blog individually, and it is not necessary and it is not recommended to modify the files inside the heart of WordPress.

 

 

 

 

 

Things to know before creating a plugin

Before you start to develop a plugin, you must first have a good knowledge of PHP programming, be familiar with WordPress features (tags, hooks, etc..) but also mastering English as the majority of resources are in English language.

How To Create a WordPress Plugin

Tips for creating WordPress plugin

Tip 1: Before developing your own plugin make sure you check the WordPress plugins directory and see that someone else has not already created the same plugin as you planning to. Also make sure that no other plugin has the same name as yours.

Tip 2: Add your plugin directory, it will be much easier for users to find the opportunity to upgrade from their administration, access to documentation, etc.. !

Tip 3: Choose a unique names for your plugin files, if a user installs it directly into /wp-content/plugins/ without creating a sub-directory, your plugin files could overwrite files that have the same names.

Tip 4: If you add your plugin to the official directory, you must create a readme.txt file that will contain various information such as the version of the plugin, with which WordPress version your plugin is compatible with, plugin description, help with installation, etc..

Tip 5: Consider the internationalization of your plugin because not everyone does speak English, and some users may wish to translate it without having to modify the source files.

Example on how to create a WordPress plugin

We will create a simple plugin that automatically adds any sentence before the beginning of each section and page.

Create a file called phrase.php and add the first line of plugin descriptions:

<?php 
/* 
  Plugin Name: Add a line of text 
  Plugin URI: http://www.webanddesigners.com/category/tutorials/wordpress-tutorials/
  Description: This plugin allows you to display a line of text at the beginning of each article.
  Version: 1.0 
  Author: Boris Zegarac 
  Author URI: http://www.webdesigncurves.com 
  License: Licence GPL2 
*/
?>

This information will be displayed in the “Extensions” in your WordPress blog, all plugins begin therefore by them.

It remains only to write the plugin, strictly speaking. We will use the “hook” to the_content that will allow to “hang” a PHP function for a specific action (i.e. the_content which is the content of an article or a page):

function hello_world($content){ 
  if(is_single()){ 
    $text = "<p>Hello World</p>"; 
    return $text.$content; 
  } 
  else{ 
    return $content; 
  } 
} 
add_action('the_content', 'hello_world');

Our “hook” will hang our function hello_world inside the content of the article/page. We recover the contents of the article/page with argument ($content) and we create a variable $text containing HTML in a single paragraph.

As I want to display my text on items and not something else like the pages, I make a simple condition using is_single() to determine whether or not I am on an article page. If it’s article page WordPress will display our variable $text and $content. If it’s not, it will return the content.

All you have to do now is upload this file phrase.php to /wp-content/plugins/ WordPress blog directory, and then activate the new plugin from the admin panel. Once the plugin is activated your words (or advertisement) will appear at the beginning of each article. This little plugin can be used for example to display a code of advertisement without changing your theme and being able to disable it by 1 click!

You are free to make any modifications to above code and start creating your own custom build WordPress plugins.

 

About the Author:

Boris Zegarac is web designer/developer and professional blogger. At free time he also writes on his web design inspiration blog dedicated to web designers where they can find inspiration in graphics, logos, templates, and more.