How to create WordPress plugins Part 1: Hello World

WordPress is the leading blogging platform used on websites. The main factor that contributed to this is the fact that WordPress is fairly simple to use. Its installation is straightforward enough that almost anybody could do it.

I do not think anyone could actually get lost on its admin dashboard cause that is also relatively simple.

Creating plugins for wordpress is also simple. That fact contributes greatly on the very rich library on wordpress plugins.

So, without complicating things, lets start creating our “Hello World” plugin.

First, you should have installed your wordpress on your localhost or your server. for a guide on how to install wordpress click here.

if you have not yet noticed, plugins installed are located on wp-content/plugins

though it is not necessary to put your plugin code file into a directory, it goes with out saying that it is recommended to do so.

so go on and create a directory under the wp-content/plugins and name it as hello_world

then lets create a php file on our hello_world directory and name it helloworld.php

so, you will now have wp-content/plugins/hello_world/helloworld.php

open up your helloworld.php using your preferred editor and insert the text below.

<?php

/*
Plugin Name: Hello World
Plugin URI: http://www.ombing.com/hello-world
Description: My Hello World Plugin
Author: Norbert Christian L. Feria
Version: 1.0
Author URI: http://www.ombing.com
*/

?>

 

of course, you could change the values of their but maintain the fields.

right after that, we will create a function that will serve as the main function to handle our plugin on the front-end.

<?php

function helloworld(content=”){

 $shortcode=”show_helloworld”;

if(!preg_match(‘|['.$shortcode.']|’, $content)){

return content;

}#if not preg_match

if(strstr($content, “[".$shortcode."]“)) {

$content =  “hello world!”;

return $content;

}#if strstr

}#function

?>

what just happened!?! well, we created a function that would identify if our predefined shortcode is in the content.. and if so, then we will show the “hello world!” on the content.

so we have the plugin file, and we have the function to handle it.. but when and how will wordpress know when to use the plugin?

for that we need to use actions or filters… in a nutshell, these are called by wordpress whenever there are activity on the site…

so, let us add the code below right after the function

<?php

add_filter(‘the_content’, ‘helloworld’);

?>

there you go, your helloworld.php is done and your plugin is ready… but before that let us first activate the plugin.. go to your wordpress admin dashboard and click on plugins->installed plugins look for the hello world plugin and click “Activate” right below the plugin name.

now that our plugin is activated, create a post or a page and on the content textarea put in the shortcode that we have specified [show_helloworld] then your done. congratulations.. when you navigate to the page or post that you have created the hello world! text will show up.

How to create WordPress plugins Part 2: Working with data and Forms

How to create WordPress plugins Part 3: Creating Admin Options


PHP, Wordpress

Leave a Reply