April 08 2014

WordPress how to create or build a new simple plugin

Tagged Under :

Wordpress
To create a plugin is a single PHP file, but common practice put the files within a directory. To begin, create a folder and naming it as your plugin name. Then, create a main PHP file and naming the file with the name of your plugin:
my-first-plugin << Folder name
- my-first-plugin.php << main PHP file name

Now we need define the plugin so the WordPress will recognize it and allow it to be installed, removed, and activated. Open the main PHP file (my-first-plugin.php), and then add the following code to the top of the file:
/*
Plugin Name: First Plugin
Plugin URI: http://chevronscode.com/
Description: -
Author: Tan
Author URI: http://chevronscode.com/
Version: 1.0.1
*/
Remember change the information for your plugin.

After define, your need build an admin menu to let the user can go your plugin with it.
add_action('admin_menu', 'register_admin_menu', 9553);

function register_admin_menu() {
    add_menu_page('First Plugin','First Plugin','activate_plugins','firstplugin','render_admin_page',plugin_dir_url(__FILE__).'firstpluginIcon.png);
}
//add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
render_admin_page is the function to retrieve the admin page.

To add submenu in your plugin, use add_submenu_page() function and add it after add_menu_page() function.
add_submenu_page('firstplugin','Entries','Entries','activate_plugins','firstplugin'.'entries', 'render_admin_option_page');

Now we can develop the frontend to display an information and data. Add the shortcode in your main plugin PHP. Register 'first-plugin' as the shortcode and 'render_front_page' as the shortcode function to call.
add_shortcode('first-plugin', 'render_front_page');

To display the information and data, add '[first-plugin]' shortcode in the following page.

First Plugin source code:
/*
Plugin Name: First Plugin
Plugin URI: http://chevronscode.com/
Description: -
Author: Tan
Author URI: http://chevronscode.com/
Version: 1.0.1
*/
add_action('admin_menu', 'register_admin_menu', 9553);
add_shortcode('first-plugin', 'render_front_page');

function register_admin_menu() {
  add_menu_page('Smart Forms','Smart Forms','activate_plugins','firstplugin','render_admin_page','');
  add_submenu_page('firstplugin','Entries','Entries','activate_plugins','firstplugin'.'entries', 'render_admin_option_page');
}

function render_front_page() {
  global $wpdb;
  $form = "Show front Form";
  return $form;
}

function render_admin_page() {
  echo "admin page";
}

function render_admin_option_page() {
  echo "admin option page";
}

Make a Comment

You must be logged in to post a comment.