simple Archives - wiki

WordPress create simple custom plugin using OOP

Create simple custom plugin using OOP

Plugin directory structure

custom plugin directory structure

Note:-Plugin folder name and the file name should be same

mysample.php

<?php 
    /*
    Plugin Name: My Sample Plugin
    Plugin URI: http://www.workassis.com
    Description: My sample plugin it will create a menu in admin and helloworld
    Author: bm
    Version: 1.0
    Author URI: https://wiki.workassis.com
    */
class SamplePlugin {
	
	public function SamplePlugin() {
		
		add_action ( 'admin_menu', array ($this, 'setup_menu' ) );

	}
	
	function setup_menu() {
		add_menu_page ( 'Sample Plugin Settings', 'Sample page', 'manage_options', 'sample-page-dashboard', array ($this,	'dashboard' ) );
		add_submenu_page ( 'sample-page-dashboard', 'Inner Page', 'Inner Page', 'manage_options', 'inner-page', array ($this,'innerpage_define' ) );
	}
	
	function dashboard() {
	    
        ?>
        <div>
       	 	<h2>Hi this is Dash board</h2>
       	 	<div>Dashboard contents will come here</div>
       	 	<div>
       	 	<?php 
       	 	
       	 	$saved_data =  get_option('save_plugin_settings');
       	 	 
       	 	$saved_data = $saved_data ? unserialize($saved_data) : null ;
       	 	
       	 	echo $saved_data && isset($saved_data['name']) && $saved_data['name'] !='' ?  "Welcome ". $saved_data['name'] : '';
       	 	
       	 	?>
       	 	</div>
        </div>
        <?php
	}
	
	function innerpage_define() {
	    
	    $saved_data =  get_option('save_plugin_settings');
	    
	    $saved_data = $saved_data ? unserialize($saved_data) : null ;
	    
	    if(isset($_POST) && isset($_POST['yourname']) ){
	        
	        $data['name'] = $_POST['yourname'];
	        
	        if($saved_data) {
	            update_option( 'save_plugin_settings',  serialize($data) );
	        }else{
	            add_option( 'save_plugin_settings',  serialize($data) );
	        }
	        
	        $saved_data['name']=$data['name'];
	    }
        ?>
        <div>
        
       	 	<h2>This is inner page</h2>
       	 	
			<form action="" method="post" >
				
				<br>
				Enter Your name<br>
				<input type="text" name="yourname" id="yourname" class="text_box" placeholder="Enter your name"  value="<?php echo $saved_data ? $saved_data['name'] : '' ; ?>" >
				<input type="submit" value="Save" >  
			</form>
		
        </div>
        <?php
	}
	
}

$masterpage_obj = new SamplePlugin ();
?>

You can see the commented line in the top of the file. This is plugin specific information header. WordPress recognize our plugin using this

<?php 
    /*
    Plugin Name: My Sample Plugin
    Plugin URI: http://www.workassis.com
    Description: My sample plugin it will create a menu in admin and helloworld
    Author: bm
    Version: 1.0
    Author URI: https://wiki.workassis.com
    */

Got admin pane and select plugin menu you can see our plugin there

plugin install

Activate the plugin.

Tadaaaaaaaa…….. see our plugin create a menu and it sub menu 🙂

activ1

activ2

activ3

 

for WordPress create advanced custom plugin using OOP https://wiki.workassis.com/wordpress-create-advanced-custom-plugin-using-oop/

 

By bm on June 23, 2016 | wordpress | A comment?
Tags: , , , ,