Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create a Wordpress Plugin

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 377
    Comment on it

    Hi there ,

    Here I am showing with the example how you can develop your custom plugin in wordpress,To develop a Plugin in Wordpress, you should know some basics concepts of Wordpress files and folders.

    So now I will create a plugin which will shows the list of students.

    1. First create the folder with the name students and put it under the wp-content/plugins/ directory
    2. Now create the file plugin.php under the students directory, in plugin.php file we will be doing all code stuff.
    3. There are some pre-made inbuilt class's functions in Wordpress with which we will play, so now in plugin.php first we will tell Wordpress that Our Plugin name is so and so...

    Put the piece of code in plugin.php

    /*
     * Plugin Name: Students List Student Example
     * Description: It will Display the lists of Students.
     * Plugin URI: http://www.abc.com
     * Author: Dinesh
     * Author URI: http://www.abc.com 
     * Version: 1.0
     * License: GPL2
     */
    

    Above code will tell wordpress that Plugin name is Students and so on. Now load your admin dashboard http://www.abc.com/wp-admin Now go to Plugins nav in the left and click on it, Now you are in the plugins list, Here you can see your Plugin named Students.

    Is'nt it was so easy, take a rest of two minutes and then get back

    1. Now you will see then there is a option to activate or delete your plugin, click on the activate link, now your plugin will be activated but here you will not be able to see your plugin in the left-nav menus, so to display your plugin in the left nav menu, you will need to use wordpress's inbuilt functions.

    put the piece of code in the same file

    if(is_admin())
    {
        new Students_Wp_List_Table();
    }
    /**
     * Students_Wp_List_Table class will create the page to load the table
     */
    class Students_Wp_List_Table{
    /**
         * Constructor will create the menu item
         */
        public function __construct()
        {
            add_action( 'admin_menu', array($this, 'Students_Lists' ));
        }
    function Students_Lists(){
    add_menu_page( 'Student List', 'Student List', 'manage_options', 'student-list-table', array($this, 'list_table_page') );
    }
    
    }
    

    Now you need to understand the meaning of the wordpress inbuilt functions is_admin(), add_action and add_menu_page()

    Start with the is_admin() function explains:- It checks that the Process is going under the admin page or not. It returns "true" if inside WordPress admin page.

    add_action($tag, $function_to_add)

    $tag is the name for the which the $function_to_add is called. $function_to_add is the call back function used.

    add_menu_page($page_title, $menu_title, $capability, $menu_slug,$function,$icon_url) It add the page to the menu lists.

    $page_title is the title of the page.
    $menu_title is the menu name
    $capability is the user role,if it is 1 means it admin has access.
    $menu_slug is the the slug name which is always be unique
    $function is the callback function name.
    $icon_url is the url of the icon which is used for this menu.

    Now with the add_action function Students_Lists function is invoked and under this function suitable parameters are passed and the list_table_page function is called.

    under the below function please write the html

    public function list_table_page()
    {
        $exampleListTable = new Example_List_Table();
        $exampleListTable = prepare_items();
      //Some html stuff here with div wrap class....
    }
    

    So now go to the plugins list and click on the activate link of your plugin and then it will be seen in the left nav menu. Click on it , you will see that you will redirected to the page Students list page, but page is empty.because we have not coded anything for the page yet.

    // WP_List_Table is not loaded automatically so we need to load it in our application
    if( ! class_exists( 'WP_List_Table' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
    }
    

    We will use class class-wp-list-table and its function to display columns, prepare items, to sort data Take a look a piece of code below and you will know how its done.

    /**
     * Create a new table class that will extend the WP_List_Table
     */
    class Students_List_Table extends WP_List_Table
    {
        /**
         * Prepare the items for the table to process
         *
         * @return Void
         */
        public function prepare_items()
        {
            $columns = $this->get_columns();
            $hidden = $this->get_hidden_columns();
            $sortable = $this->get_sortable_columns();
    
            $data = $this->table_data();
            usort( $data, array( &$this, 'sort_data' ) );
    
            $perPage = 20;
            $currentPage = $this->get_pagenum();
            $totalItems = count($data);
    
            $this->set_pagination_args( array(
                'total_items' => $totalItems,
                'per_page'    => $perPage
            ) );
    
            $data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
    
            $this->_column_headers = array($columns, $hidden, $sortable);
            $this->items = $data;
        }
    
        /**
         * Override the parent columns method. Defines the columns to use in your listing table
         *
         * @return Array
         */
        public function get_columns()
        {
            $columns = array(
                'id'          => 'ID',
                'student_name'       => 'Student Name',
                'father_name' => 'Father Name',
                'rollno'        => 'Roll No',
                'date_of_birth'    => 'DOB',
                'gender'      => 'male'
            );
    
            return $columns;
        }
    
        /**
         * Define which columns are hidden
         *
         * @return Array
         */
        public function get_hidden_columns()
        {
            return array();
        }
    
        /**
         * Define the sortable columns
         *
         * @return Array
         */
        public function get_sortable_columns()
        {
            return array('title' => array('title', false));
        }
    
        /**
         * Get the table data
         *
         * @return Array
         */
        private function table_data()
        {
            $data = array();
    
            $data[] = array(
                        'id'          => 1,
                        'student_name'       => 'Raj Singh',
                        'father_name' => 'Mr B.K Singh ',
                        'rollno'        => '101',
                        'date_of_birth'    => '01-Aug-1990',
                        'gender'      => 'male'
                        );
    
            $data[] = array(
                        'id'          => 2,
                        'student_name'       => 'Manoj Rawat',
                        'father_name' => 'Mr D.M Rawat',
                        'rollno'        => '102',
                        'date_of_birth'    => '10-jan-1980',
                        'gender'      => 'male'
                        );
    
            $data[] = array(
                       'id'          => 3,
                        'student_name'       => 'Pinki Negi',
                        'father_name' => 'Mr C.K Negi',
                        'rollno'        => '103',
                        'date_of_birth'    => '02-Feb-1991',
                        'gender'      => 'female'
                        );
    
            $data[] = array(
                        'id'          => 4,
                        'student_name'       => 'Rahul Kumar',
                        'father_name' => 'Mr B.S Kumar',
                        'rollno'        => '104',
                        'date_of_birth'    => '10-Dec-1990',
                        'gender'      => 'male'
                        );
    
            $data[] = array(
                        'id'          => 5,
                        'student_name'       => 'xyz',
                        'father_name' => 'Mr xyz ',
                        'rollno'        => '105',
                        'date_of_birth'    => '01-Aug-1990',
                        'gender'      => 'male'
                        );
    
            return $data;
        }
    
        /**
         * Define what data to show on each column of the table
         *
         * @param  Array $item        Data
         * @param  String $column_name - Current column name
         *
         * @return Mixed
         */
        public function column_default( $item, $column_name )
        {
            switch( $column_name ) {
                case 'id':
                case 'student_name':
                case 'father_name':
                case 'rollno':
                case 'date_of_birth':
                case 'gender':
                    return $item[ $column_name ];
    
                default:
                    return print_r( $item, true ) ;
            }
        }
    
        /**
         * Allows you to sort the data by the variables set in the $_GET
         *
         * @return Mixed
         */
        private function sort_data( $a, $b )
        {
            // Set defaults
            $orderby = 'student_name';
            $order = 'asc';
    
            // If orderby is set, use this as the sort column
            if(!empty($_GET['orderby']))
            {
                $orderby = $_GET['orderby'];
            }
    
            // If order is set use this as the order
            if(!empty($_GET['order']))
            {
                $order = $_GET['order'];
            }
    
    
            $result = strnatcmp( $a[$orderby], $b[$orderby] );
    
            if($order === 'asc')
            {
                return $result;
            }
    
            return -$result;
        }
    }
    

    Now load your plugin page,Hope you will enjoy

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: