Home» »How to create a simple wordpress plugin

How to create a simple wordpress plugin

Plugin is one of the mainstay features of wordpress. Wordpress plugin will allow the users to use an function or script that written by a developer with easier, because the script can be packaged in an application that can be used by bloggers just by install it, with a single click.

For some bloggers and newbie developers might want to know how to create a plugin for wordpress, and here I will show you how to create a simple wordpress plugin.

For the first step, i want you to make a folder in the plugins folder. wp-content/plugins/. In this tutorial I named my plugin folder my-first-plugin.

Creating Main Plugin's File

Inside this file, it's containing information of the plugin. Like plugin name, author, authors address, address plugin, license, etc. And now, I want you to create a php file that contains the information.

Here I named it my-first-plugin.php. This file will be the main file of our plugin. Fill the file with the plugin's information below.

<?php

/*
   Plugin Name: My First Plugin
   Plugin URI: http://my-first-plugin.com
   Description: this is my first plugin
   Version: 1.0
   Author: Rahman Kurnia
   Author URI: http://classofbeginner.blogspot.com
   License: GPL2
*/

Now we have a new plugin in the installed plugins list, in the wordpress admin Dashboard >> Plugins. Let's activate it to check if there is a miracle to our plugin, lol.


See, make a wordpress plugin it's very easy, but your plugin is not done yet. We need an option page, and it's should show something to the templates. Just follow the next steps.

Creating Plugin Options Page

The first thing you should do before you write the code for your plugin is to define the address and path of your plugin, to simplify writing the code and functionality of the plugins.

This will really help when we write a lot of code and have lots of directories in the plugin folder. We can also define the directory addresses each. Suppose that the path to the directory includes.

define( 'MFP_PATH', plugin_dir_path(__FILE__) );
define( 'MFP_DIR', plugin_dir_url(__FILE__) );

MFP is my first plugin. And now, every time you want to call your plugin, you can write the address like this

<?php echo MFP_DIR . ('process.php'); ?>

If you have another directory in your plugin folder, example includes, img, js, or css folders then you can write it like this

<?php echo MFP_DIR . ('includes/process.php'); ?>

Next. Let's create the plugin option page. Enter the code below to make an admin page that have some options to your plugin.

function mfp_admin_page(){
 global $mfp_settings;
 $mfp_settings = add_menu_page(__('Admin MFP', 'mfp'),__('MFPanel','mfp'),'manage_options','mfp-admin-panel','mfp_render_admin');
}
add_action('admin_menu','mfp_admin_page');

$mfp_settings is the hook name of the function. This will be used to make the load hook scripts for this plugin.

Parameter 1 (Admin MFP) - is the title used in your HTML page.
Parameter 2 (MFPanel) - is the title used for the menu item on the left menu.
Parameter 3 (manage_options) - is the capability needed to add the page. You can keep it as administrator in most of the cases..
Parameter 4 (mfp-admin-panel) - is a unique key used for the menu item.
Parameter 5 (mfp_render_admin) - is the function name used to implement the HTML form.

Now let's refresh the dashboard to see if the new menu appears at the very bottom. It should appear as MFPanel menu, to change the name of the menu, you can change the parameter 2.


Render the Plugin Admin Options Page


To create a html page on the plugin options page, you have to use parameter 5 as a new function. Let's see what's on the parameter 5, ah, it's mfp_render_admin. Now I want you to write code below to create html of your plugin options page.

function mfp_render_admin(){
     ?>

<div class="wrap">
<h2><?php _e('MFP Admin Panel','mfp'); ?></h2>

<p>Hello world! This is my first plugin</p>

</div>

     <?php
}

You can create a form and input on the page by adding some html code into it.


Load the scripts for admin page

To load scripts like css and js for the admin page, you can use the code below as an example.

function mfp_load_scripts($hook){
 global $mfp_settings;
 
 if( $hook != $mfp_settings )
 return;
 
 wp_enqueue_script('mfp-ajax', plugin_dir_url(__FILE__) . 'js/mfp-ajax.js'); 
 
 wp_register_style( 'main-style', plugins_url('css/style.css', __FILE__) );
 wp_enqueue_style( 'main-style' );
 
}
add_action('admin_enqueue_scripts','mfp_load_scripts');

Done, that's how to create simple wordpress plugin, if there are any questions please comment below.

About Author:

Rahman Kurnia is a blogger, SEO fighter, who loves PHP, CSS, HTML and interested in js, ajax, c++ and much more. He love writing something informatif to help others, and to keep his knowledge on the internet.

Follow him @ Twitter | Facebook | Google +

0 comments:

Post a Comment

Like us on Facebook
Follow us on Twitter
Recommend us on Google Plus
Blog Widget by LinkWithin