how to make simple plugin in wordpress

0
8629

Overview

how to make basic helloworld plugin in wordpress
how to add custom menu in wordpress admin panel
how to use hooks in wordpress
how to set_option works in wordpress plugin
how to show get_option values in frontend with wp_footer hook
how to make sub menu and menu pages in admin panel
this video has answers of all these above’s questions.

Code Snippet

<?php
add_action('admin_menu', 'my_admin_menu');

function my_admin_menu () {
	//parameters details
	 //add_management_page( $page_title, $menu_title, $capability,
 $menu_slug, $function );
	 //add a new setting page udner setting menu
  //add_management_page('Footer Text', 'Footer Text', 'manage_options',
 __FILE__, //'footer_text_admin_page');
  //add new menu and its sub menu 
  add_menu_page('Footer Text title', 'Footer Settings', 'manage_options',
 'footer_setting_page', 'footer_text_admin_page');
add_submenu_page( 'footer_setting_page', 'Page title', 'Sub-menu title', 
'manage_options', 'child-submenu-handle', 'my_magic_function');
}

function footer_text_admin_page () {
  echo 'this is where we will edit the variable';
}

// mt_settings_page() displays the page content for the Test Settings submenu
function mt_settings_page() {
    echo "<h2>" . __( 'Footer Settings Configurations', 'menu-test' ) . "</h2>";
	include_once('footer_settings_page.php');
}
?>
<?php
    global $chk;
    if(isset($_POST['wphw_submit'])){
            wphw_opt();
    }
    function wphw_opt(){
        $hellotxt = $_POST['footertextname'];
        global $chk;
        if( get_option('footer_text') != trim($hellotxt)){
            $chk = update_option( 'footer_text', trim($hellotxt));
        }
    }
?>
<div class="wrap">
  <div id="icon-options-general" class="icon32"> <br>
  </div>
  <h2>Footer Settings</h2>
  <?php if(isset($_POST['wphw_submit']) && $chk):?>
  <div id="message" class="updated below-h2">
    <p>Content updated successfully</p>
  </div>
  <?php endif;?>
  <div class="metabox-holder">
    <div class="postbox">
      <h3><strong>Enter footer text and click on save button.</strong></h3>
      <form method="post" action="">
        <table class="form-table">
          <tr>
            <th scope="row">Footer Text</th>
            <td><input type="text" name="footertextname" 
value="<?php echo get_option('footer_text');?>" style="width:350px;" /></td>
          </tr>
          <tr>
            <th scope="row">&nbsp;</th>
            <td style="padding-top:10px;  padding-bottom:10px;">
<input type="submit" name="wphw_submit" value="Save changes" class="button-primary" />
</td>
          </tr>
        </table>
      </form>
    </div>
  </div>
</div>

<?php
function your_function() {
   echo "<div style='color: red;
    font-size: 30px;
    margin: 20px;'>".get_option('footer_text')."</div>";
}
add_action( 'wp_footer', 'your_function' );
?>

 

Summary

basic helloworld plugin in wordpress,how to add custom menu in wordpress admin dashboard,hooks in wordpress,use of set_option and get_option in wordpress