WordPress Plugin Development Guide for Beginner’s: A hello world plugin

Published on : May 16, 2013

Author:

Category: Wordpress


Hello Folks ! Today I will share tips on how easily you can create a wordpress plugin. Let’s dig into main things rather than describing too much stuff.

First choose a unique name for your plugin. Here I will name it as WP Hello World. Now create a PHP file and name it as ‘wp-hello-world.php’. It will be your main plugin file.

Now you have to put some information into the top of your main plugin PHP file. It’s important and it will be served as standard plugin information header. This header lets WordPress recognize that your plugin exists, add it to the plugin management screen in order it to be activated, load it up, and run its functions; without the header, your plugin will never be activated and will never run. Here is the standard header information format:


<?php
/*
	Plugin Name: WP Hello World
	Plugin URI: www.https://dev.cybernetikz.com
	Description: A simple hello world plugin for beginners.
	Version: 1.0.0
	Author: Cybernetikz
	Author URI: www.https://dev.cybernetikz.com
	License: GPL2
*/
?>

Put your main PHP file into a directory and name it ‘wp-hello-world’. Now put the directory into wordpress plugins directory. It will look like the following screenshot for localhost.

Now go to admin panel plugins management screen and you will find your newly created plugins.

Now time to add some functionality. First add a menu tab for your plugin. Create another PHP file for managing menu page content and name it ‘wphw-admin.php’. Then insert the following code into your main plugin file.


<?php
	function wp_hello_world_menu(){
		add_menu_page( 'A hello world plugin for beginners', 'HelloWorld', 'manage_options', 'wphw', 'wphw_opt_admin' );
	}

	add_action('admin_menu','wp_hello_world_menu');

	function wphw_opt_admin(){
		include_once('wphw-admin.php');
	}
?>

Activate your plugin and you will get menu tab as the following screenshot.

Now put the following code into ‘wphw-admin.php’.


<?php
	global $chk;
	if(isset($_POST['wphw_submit'])){
			wphw_opt();
	}
	function wphw_opt(){
		$hellotxt = $_POST['hello-text'];
		global $chk;
		if( get_option('hello_text') != trim($hellotxt)){
			$chk = update_option( 'hello_text', trim($hellotxt));
		}
	}
?>
<div class="wrap">
  <div id="icon-options-general" class="icon32"> <br>
  </div>
  <h2>WP Hello World Settings</h2>
  <?php if(isset($_POST['wphw_submit']) && $chk):?>
  <div id="message" class="updated below-h2">
    <p>Content updated successfully</p>
  </div>
  <?php endif;?>
  <div class="metabox-holder">
    <div class="postbox">
      <h3><strong>Hello content option</strong></h3>
      <form method="post" action="">
        <table class="form-table">
          <tr>
            <th scope="row">Hello Text</th>
            <td><input type="text" name="hello-text" value="<?php echo get_option('hello_text');?>" style="width:350px;" /></td>
          </tr>
          <tr>
            <th scope="row">&nbsp;</th>
            <td style="padding-top:10px;  padding-bottom:10px;"><input type="submit" name="wphw_submit" value="Save changes" class="button-primary" /></td>
          </tr>
        </table>
      </form>
    </div>
  </div>
</div>

Click menu tab ‘HelloWorld’. You will get the following screen

Now I will create a simple function and also a shortcode to show the hello world text into the page or post. Add the following code into main plugin PHP file.


<?php
function show_text(){
    echo get_option('hello_text');
}

function show_hello_text() {
    return get_option('hello_text');
}

add_shortcode('showtext', 'show_hello_text');
?>

Use the shortcode [showtext] into page or post content to show your hello world text.


Optionally you can use the function show_text() as template tag in your theme to show the hello world text.


Leave a Reply

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