Create A Custom Form Plugin In WordPress - Part Two

Please read my first part: 

Today, you will learn how to write a code in a WordPress plugin. WordPress provides plugin features for developers. Plugin means an individual module. WordPress creates multiple plugins. If you download a plugin, the link will be available at wordpress plugin. Click the link and search "use plugin." Today, I will discuss how to develop WordPress plugins, write the code, and install the plugin.

Download the code, given previously, and install again,



Let's start writing code into the helloworld plugin.

First, install the plugin in Wordpress. When the download is complete, open Wordpress administration panel, as shown below:
 
 
  

My Wordpress plugin will show in the plugin list:
 
 
Whenever a Wordpress plugin is installed into Wordpress, open the plugin code.

Wordpress Plugin Path: C:\xampp1\htdocs\wordpress\wp-content\plugins

Below screen



Open helloworld.php file into the helloworld folder and write the code, shown in the example:
  1. <?php   
  2.     /* 
  3.     Plugin Name: hello world 
  4.     Plugin URI: http://www.wordpress.org 
  5.     Description: Create plugin 
  6.     Author: bhushan 
  7.     Version: 1.0 
  8.     Author URI: http://www.xyz.com 
  9.     */  
  10. class MyPlugin{  
  11.     
  12.       private $my_plugin_screen_name;  
  13.       private static $instance;  
  14.        /*......*/  
  15.     
  16.       static function GetInstance()  
  17.       {  
  18.             
  19.           if (!isset(self::$instance))  
  20.           {  
  21.               self::$instance = new self();  
  22.           }  
  23.           return self::$instance;  
  24.       }  
  25.         
  26.       public function PluginMenu()  
  27.       {  
  28.        $this->my_plugin_screen_name = add_menu_page(  
  29.                                         'My Plugin',   
  30.                                         'My Plugin',   
  31.                                         'manage_options',  
  32.                                         __FILE__,   
  33.                                         array($this'RenderPage'),   
  34.                                         plugins_url('/img/icon.png',__DIR__)  
  35.                                         );  
  36.       }  
  37.         
  38.       public function RenderPage(){  
  39.        ?>  
  40.        <div class='wrap'>  
  41.         <h2>custom form</h2>  
  42.         <div class="main-content">  
  43.   
  44.         <!-- You only need this form and the form-basic.css -->  
  45.   
  46.         <form class="form-basic" method="post" action="#">  
  47.   
  48.             <div class="form-title-row">  
  49.                 <h1>Form Example</h1>  
  50.             </div>  
  51.   
  52.             <div class="form-row">  
  53.                 <label>  
  54.                     <span>Full name</span>  
  55.                     <input type="text" name="name">  
  56.                 </label>  
  57.             </div>  
  58.   
  59.             <div class="form-row">  
  60.                 <label>  
  61.                     <span>Email</span>  
  62.                     <input type="email" name="email">  
  63.                 </label>  
  64.             </div>  
  65.   
  66.             <div class="form-row">  
  67.                 <label>  
  68.                     <span>Dropdown</span>  
  69.                     <select name="dropdown">  
  70.                         <option>Option One</option>  
  71.                         <option>Option Two</option>  
  72.                         <option>Option Three</option>  
  73.                         <option>Option Four</option>  
  74.                     </select>  
  75.                 </label>  
  76.             </div>  
  77.   
  78.             <div class="form-row">  
  79.                 <label>  
  80.                     <span>Textarea</span>  
  81.                     <textarea name="textarea"></textarea>  
  82.                 </label>  
  83.             </div>  
  84.   
  85.             <div class="form-row">  
  86.                 <label>  
  87.                     <span>Checkbox</span>  
  88.                     <input type="checkbox" name="checkbox" checked>  
  89.                 </label>  
  90.             </div>  
  91.   
  92.             <div class="form-row">  
  93.                 <label><span>Radio</span></label>  
  94.                 <div class="form-radio-buttons">  
  95.   
  96.                     <div>  
  97.                         <label>  
  98.                             <input type="radio" name="radio">  
  99.                             <span>Radio option 1</span>  
  100.                         </label>  
  101.                     </div>  
  102.   
  103.                     <div>  
  104.                         <label>  
  105.                             <input type="radio" name="radio">  
  106.                             <span>Radio option 2</span>  
  107.                         </label>  
  108.                     </div>  
  109.   
  110.                     <div>  
  111.                         <label>  
  112.                             <input type="radio" name="radio">  
  113.                             <span>Radio option 3</span>  
  114.                         </label>  
  115.                     </div>  
  116.   
  117.                 </div>  
  118.             </div>  
  119.   
  120.             <div class="form-row">  
  121.                 <button type="submit">Submit Form</button>  
  122.             </div>  
  123.   
  124.         </form>  
  125.   
  126.     </div>  
  127.           
  128.        </div>  
  129.        <?php  
  130.       }  
  131.   
  132.       public function InitPlugin()  
  133.       {  
  134.            add_action('admin_menu'array($this'PluginMenu'));  
  135.       }  
  136.     
  137.  }  
  138.    
  139. $MyPlugin = MyPlugin::GetInstance();  
  140. $MyPlugin->InitPlugin();  
  141. ?> 
It will look as shown below:
 


Similar Articles