a digital blog

PHP, Tutorial

How to create WordPress Plugin

How to create WordPress Plugin

This post explains you about how to create a wordpress plugin,how to  install & uninstall the plugin, how to create a plugin Admin menu .

 

 

Download Link

 

Here are some basic steps to create a plugin called contact_us,   that will shows data in admin panel.

  1. Create a folder contact_us  in wp-content/plugins
  2. Create a file contact_us.php and open it. Now you need to put some basic plugin details and remember wordpress will recognize you by this information.
    /*
    Plugin Name:Contact Us Plugin
    Plugin URI: http://www.91weblessons.com
    Description: WordPress Contact Us plugin
    Author: Vishal Gupta
    Version: 1.0.1
    Author URI: http://www.91weblessons.com
    */
  3. Now go to the admin and click Plugins. You will see your plugin is in the listing.
    How to create WordPress Plugin
  4. Now the time to set database table setting , which will created dynamically at time of plugin installation. And also dropped at uninstallation.
    global $wpdb;
    $pro_table_prefix=$wpdb->prefix;
    define('PRO_TABLE_PREFIX', $pro_table_prefix);
  5. Now time to set up functions for plugin installation and uninstallation.
    register_activation_hook(__FILE__,'contact_us_install');
    register_deactivation_hook(__FILE__ , 'contact_us_uninstall' );
    add_action('admin_menu','contact_us_admin_menu');
    // It will tells to wordpress, calls contact_us_admin_menu
    after plugin installation function
    
    function contact_us_install()
    {
        global $wpdb;
        $table = PRO_TABLE_PREFIX."contact_us";
        $structure = "CREATE TABLE $table (
            id INT(11) NOT NULL AUTO_INCREMENT,
            name VARCHAR(80) NOT NULL,
            email VARCHAR(50) NOT NULL,
    	UNIQUE KEY id (id)
        );";
        $wpdb->query($structure);
    }
    function contact_us_uninstall()
    {
        global $wpdb;
        $table = PRO_TABLE_PREFIX."contact_us";
        $structure = "drop table if exists $table";
        $wpdb->query($structure);
    }
  6. Now save the code and go to the admin and click Plugins and activate Contact Us plugin and go to phpmyadmin and your table now list here.How to create WordPress Plugin
  7. Now time to set admin section menu.
    function contact_us_admin_menu() {
        add_menu_page("Contact Us","Contact Us",8,__FILE__,"contact_us_admin_menu_list");
    }
    // It will tells to wordpress, to shows Contact Us link  in admin panel and
    whenever any one click into it call contact_us_admin_menu_list function.
    
    function contact_us_admin_menu_list() {
         include 'contact_us_admin_listing.php';
    }
  8. Now create a file called contact_us_admin_listing.php into same folder  and put data display code into the file.
    <?php
    $columns = array(
    'Name' => 'Name',
    'Email' => 'Email'
    );
    register_column_headers('pro-list-site', $columns);
    
    global $wpdb;
    $sql = "SELECT *FROM ".PRO_TABLE_PREFIX."contact_us where 1";
    $results = $wpdb->get_results($sql);
    ?>
    <div class="wrap">
    <?php echo "<h2>" . __( 'Contact Us Data' ) . "</h2>"; ?>
    <table class="widefat page fixed" cellspacing="0">
    <thead>
    <tr>
    <?php print_column_headers('pro-list-site'); ?>
    </tr>
    </thead>
    
    <tfoot>
    <tr>
    <?php print_column_headers('pro-list-site', false); ?>
    </tr>
    </tfoot>
    
    <tbody>
    <?php
    if(count($results) > 0)
    {
    foreach($results as $result)
    {
    echo "<tr><td>".$result-> name."</td><td>".$result->email."</td></tr>";
    }
    }else{
    echo '<tr><td colspan="2">No Data Found</td></tr>';
    }
    ?>

    And here is the output.

    How to create WordPress Plugin

    No Data Found because right now contact_us table is blank. Just go to phpmyadmin and add some data into table and you will see that data here.

    How to create WordPress Plugin

    And go to contact us plugin section and refresh your page and you will see your data here now.

    How to create WordPress Plugin

  9. Using add_shortcode function you can shows plugin into front end. That I will explain you in my next tutorial.

9 Comments

  1. Bernard

    Cool

  2. Eddy

    nice tuts…everything make easy now….

  3. Divya Goel

    Working super fine.
    Thankyou. :)

  4. sikis izle

    Awesome things.

    Thank you a lot

  5. Rahul Desai

    It’s really a great and helpful piece of info.

  6. Thomas Pereira

    A big thank you for your blog.Really thank you!

  7. Muhammad Shoaib Alam

    I really enjoyed reading this post

  8. Gagan Sofyan

    u always share good article, i like it

  9. John Dep

    This is pretty cool…Thanks a lot.
    Waiting for part 2


*

Copyright © 91 Web Lessons - a digital blog