Monday, 14 December 2015

How to create a Basic Wordpress Plugin

Process :

#1. I am assuming that you have installed wordpress already .
If Yes : go to wordpress directory find wp-content > plugins > myCustomPlugin (create this folder)

#2. Go inside myCustomPlugin folder and create a init.php file and write same as displaying below :
 
 
<?php
   /*
   Plugin Name: My Custom Plugin
   Plugin URI: http://mycustomplugin.com
   Description: A basic plugin.
   Version: 1.2
   Author: Ankur Vishwakarma
   License: GPL2
   */

add_action('admin_menu','myPluginInit'); // Attach you plugin with wp-admin sidebar


function myPluginInit (){

add_menu_page( 'My Custom Plugin', 'My Custom Plugin', 'manage_options', 'mycustomplugin', 'my_custom_menu_page','', 6 );  // create a menu for you plugin in side admin bar same as page, post etc.

 }



function my_custom_menu_page(){

$fname=get_option('cus_firstname',$_POST['cus_firstname']); //get entries from wp_options table if already exist
$lname=get_option('cus_lastname',$_POST['cus_lastname']);

if(!empty($fname) && !empty($lname)){
$h2="$fname $lname exist";
}
else{
$h2="No user exist";
}
if(isset($_POST['set'])){

if(empty($fname) && empty($lname)){
add_option('cus_firstname',$_POST['cus_firstname']); // save entries in wp_options table if not exist
add_option('cus_lastname',$_POST['cus_lastname']);
}else{
update_option('cus_firstname',$_POST['cus_firstname']); // update entries in wp_options table if already exist
update_option('cus_lastname',$_POST['cus_lastname']);
$h2=$_POST['cus_firstname'].' '.$_POST['cus_lastname'] .'has been updated';
}
    }


 ?>

<h2><?php echo $h2; ?></h2>
<form action="" method="post">
<label>User First Name :</label><input type="text" name="cus_firstname"><br>
<label>User Last Name :</label><input type="text" name="cus_lastname"><br>
<input type="submit" value="Set" name="set">
</form>

 <?php

}//end my_custom_menu_page



 ?>

1 comment: