wp_list_table Bulk Actions Example |
X

Congrats, You are Subscribed to Receive Updates.

wp_list_table Bulk Actions Example


Before I am getting into the topic, Here I will give you the source code which i worked previously. This is for your reference and example.

<?php
/*
Plugin Name: Wp_list_table_Edit by Kv Varadha
Plugin URI: https://kvcodes.com	
Description: A Simple Way to make your Plugin from Pluginpress. i.e Create your custom plugin from WordPress Admin itself. No need of much coding. 
Version: 1.0
Author: Kv Varadha 
Author URI: https://plus.google.com/u/0/b/103705444781603570835/
*/
define('KV_PLUGIN_URL', plugin_dir_url( __FILE__ ));


/*****************************************
*  admin menu creation for pluginpress 
****************************************/
function kv_pluginpress_menu(){ 
	add_menu_page('PluginPress', 'PluginPress', 'manage_options', 'pluginpress' , 'kvcodes_edit_plugins', KV_PLUGIN_URL.'/images/kv_logo.png', 66);
	add_submenu_page( 'pluginpress', 'Create New ', 'Create New', 'manage_options', 'kvcodes', 'kvcodes_edit_plugin' );	
	//add_action("load-{$settings_page}", 'kv_load_settings_page');
} add_action('admin_menu', 'kv_pluginpress_menu');


if(!class_exists('WP_List_Table')){
    require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class KV_PluginPress extends WP_List_Table {  

    function __construct(){
        global $status, $page;
                
        //Set parent defaults
        parent::__construct( array(
            'singular'  => 'plugin',     //singular name of the listed records
            'plural'    => 'plugins',    //plural name of the listed records
            'ajax'      => false        //does this table support ajax?
        ) );        
    }
	
	
	function extra_tablenav( $which ) {
		if ( $which == "top" ){		
			echo" Your Plugins List created by using  Pluginpress  ";
		}		
	}

    function column_default($item, $column_name){
        switch($column_name){
            case 'description':
            case 'author':
			case 'name' :
			case 'version' : 
                return $item[$column_name];
            default:
                return print_r($item,true); //Show the whole array for troubleshooting purposes
        }
    }
	 function get_columns(){
        $columns = array(
            'cb'        => '<input type="checkbox" />', //Render a checkbox instead of text
            'name'     => 'Name',
            'description'    => 'Description ',
			'version' => 'Version',
            'author'  => 'Author'
        );
        return $columns;
    }

    function get_sortable_columns() {
        $sortable_columns = array(
            'name'     => array('name',false),     //true means it's already sorted
            'description'    => array('description',false),
            'author'  => array('author',false)
        );
        return $sortable_columns;
    }

    function column_name($item){       
       
        $actions = array(
            'edit'      => sprintf('<a href="?page=%s&action=%s&plugin=%s">Edit</a>','kvcodes','edit',$item['ID']),
            'delete'    => sprintf('<a href="?page=%s&action=%s&plugin=%s">Delete</a>','kvcodes','delete',$item['ID']),
        );        
       
        return sprintf('%1$s <span style="color:silver ; display : none;">(id:%2$s)</span>%3$s',
            /*$1%s*/ $item['name'],
            /*$2%s*/ $item['ID'],
            /*$3%s*/ $this->row_actions($actions)
        );
    }

    function column_cb($item){
        return sprintf(
            '<input type="checkbox" name="%1$s[]" value="%2$s" />',
            /*$1%s*/ $this->_args['singular'],  //Let's simply repurpose the table's singular label ("plugin")
            /*$2%s*/ $item['ID']                //The value of the checkbox should be the record's id
        );
    }
    function get_bulk_actions() {
        $actions = array(
            'delete'    => 'Delete'
        );
        return $actions;
    }

    function process_bulk_action() {        
      
        if( 'delete'===$this->current_action() ) {
          //  wp_die('Items deleted (or they would be if we had items to delete)!');
           foreach($_GET['id'] as $id) {
                //$id will be a string containing the ID of the video
                //i.e. $id = "123";                
                delete_this_video($id);
            }
        }        
    }

    function prepare_items() {
        global $wpdb; 
		$pluginpress_db= $wpdb->prefix.'kv_pluginpress' ;    
        $kv_query = "SELECT * FROM $pluginpress_db ";
        $per_page = 5;
        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
		$this->_column_headers = array($columns, $hidden, $sortable);       
        $this->process_bulk_action();        
        $data = $wpdb->get_results($kv_query, ARRAY_A);
        function usort_reorder($a,$b){
            $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'ID'; //If no sort, default to name
            $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'dsc'; //If no order, default to asc
            $result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order
            return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
        }
        usort($data, 'usort_reorder');        
        $current_page = $this->get_pagenum();       
        $total_items = count($data);  
        $data = array_slice($data,(($current_page-1)*$per_page),$per_page);        
        $this->items = $data;        
        $this->set_pagination_args( array(
            'total_items' => $total_items,                  //WE have to calculate the total number of items
            'per_page'    => $per_page,                     //WE have to determine how many items to show on a page
            'total_pages' => ceil($total_items/$per_page)   //WE have to calculate the total number of pages
        ) );
    }
}

function kvcodes_edit_plugins(){  
	if(isset($_GET['action'] ) && $_GET['action'] == 'edit' &&  isset($_GET['plugin']) && $_GET['plugin']  != null ){  
		$id = $_GET['plugin'] ; 
		kv_edit_selected_plugin($id);  
	} else if(isset($_GET['action'] ) && $_GET['action'] == 'delete' &&  isset($_GET['plugin']) && $_GET['plugin']  != null ){  
		$id = $_GET['plugin'] ; 
		kv_delete_selected_plugin($id); 
	} else { 
		$PluginPress = new KV_PluginPress();
		$PluginPress->prepare_items();       ?>
		<div class="wrap">        
			<div id="icon-users" class="icon32"><br/></div>
			<h2>PluginPress <a href="<?php echo admin_url('admin.php?page=kvcodes');  ?>" class="add-new-h2">Add New</a></h2> 
			<form id="plugins-filter" method="get">        
				<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />          
				<?php $PluginPress->display() ?>
			</form>        
		</div>
		<?php
	}    
}


function kv_edit_selected_plugin($id) {
	// Edit function goes here.
}

funciton kv_delete_selected_plugin($id) {
	// Delete function goes here.	
}

The above one is the complete code to create row actions. This is one of my convenient way to use use row actions.  Here my db name is

” wp_kv_pluginpress. ” here i listed all the plugins. this is just for understanding.  and “kvcodes_edit_plugins” is the main function to create object and perform row actions too.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

3 comments

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar