• Hi,
    I’m trying to do a plugin which uses custom table and custom post type with custom taxonomies. To show the data from the custom table I’m extending the wp_list_table class. The data is shown correctly, but my trouble starts when I want to delete the record or do anything on that page. I’m trying to implement the bulk delete and also the mouse over delete but it’s just not working. The mouse over delete “button” is not appearing at all and when I click the apply button for the bulk delete it just redirects me to a You do not have sufficient permissions to access this page. I don’t know what I’m doing wrong. Any ideas?
    The code for the delete:

    function column_name( $item ) {
    
    		$delete_nonce = wp_create_nonce( 'bsp_delete_student' );
    
    		$title = '<strong>' . $item['students_name'] . '</strong>';
    
    		$actions = [
    			'delete'=>sprintf('<a href="?post_type=%s&page=%s&action=%s&student=%s">Delete</a>', $_REQUEST['post_type'], $_REQUEST['page'], 'delete', $item->students_id)
    		];
    
    		return $title . $this->row_actions( $actions );
    	}
    
    	function get_bulk_actions(){
    		$actions=array(
    		'delete'=>__( 'Delete' )
    		);
    		return $actions;
    
    	}
    public function process_bulk_action() {
    
    		//Detect when a bulk action is being triggered...
    		if ( 'delete' === $this->current_action() ) {
    
    			// In our file that handles the request, verify the nonce.
    			$nonce = esc_attr( $_REQUEST['_wpnonce'] );
    
    			if ( ! wp_verify_nonce( $nonce, 'bsp_delete_student' ) ) {
    				die( 'Go get a life script kiddies' );
    			}
    			else {
    				self::delete_student( absint( $_GET['student'] ) );
    
    				wp_redirect( esc_url( add_query_arg() ) );
    				exit;
    			}
    
    		}
    
    		// If the delete bulk action is triggered
    		if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'delete' )
    		     || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'delete' )
    		) {
    
    			$delete_ids = esc_sql( $_POST['delete'] );
    
    			// loop over the array of record IDs and delete them
    			foreach ( $delete_ids as $id ) {
    				self::delete_student( $id );
    
    			}
    
    			wp_redirect( esc_url( add_query_arg() ) );
    			exit;
    		}
    	}
    
    	public static function bsp_delete_student($id){
    		global $wpdb;
    		$wpdb->delete(
    			"{$wpdb->prefix}students",
    			[ 'students_id' => $id ],
    			[ '%d' ]
    		);
    	}

    I’ve changed the code a couple of times for the delete function but I always get the same error message. Any help would be greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter dea_om

    (@dea_om)

    Hi, so I’ve managed to resolve the error of not having sufficient permissions to access the page. In the form I changed the method from get to post.
    But the question of why the mouse over (row action) and delete are not working still remains.

    Hai,

    You’ve called your function “column_name“.
    In that function, I see you use the item field “$item[‘students_name‘]”.
    Unless you also have and defined a column for “$item[‘name‘]”, this doesn’t work.

    You should try to rename your function “column_name” to function “column_students_name“.
    The syntax for those functions is “column_$custom( $item )”, see the definition of WP_List_Table.

    Hope this helps.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_list_table bulk action not working’ is closed to new replies.