0

I have an admin page which I added using add_submenu_page function which is listing some custom data using WP_List_Table class. I have defined the bulk actions options in get_bulk_actions and it appears in dropdown. But which is the hook that will be triggered when user submits the form.

I tried following but no luck

add_action( 'admin_action_YOUR_ACTION', 'your_bulk_action_handler_function' );

Now in order to make it work I defined a hook function in 'admin_init' and am checking if(isset($_POST['action']) && $_POST['action'] == 'export_action' ) { //Export code here. }

But is there a better way of doing it?

0

You have to use "process_bulk_action" function like this:

public function process_bulk_action() {

   //nonce validations,etc

    $action = $this->current_action();

    switch ( $action ) {

        case 'export_action':

            // Do whatever you want
            wp_redirect( esc_url( add_query_arg() ) );
            break;

        default:
             // do nothing or something else
            return;
            break;
        }

        return;

    }
}

If you need a complete example this is the code I'm using for a plugin:

   public function process_bulk_action() {

        if ( isset( $_POST['_wpnonce'] ) && ! empty( $_POST['_wpnonce'] ) ) {
            echo '<pre>';
            print_r($_POST);
            echo '</pre>';

            $nonce  = filter_input( INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING );
            $action = 'bulk-' . $this->_args['plural'];

            if ( ! wp_verify_nonce( $nonce, $action ) )
                wp_die( 'Nope! Security check failed!' );

        }

        $action = $this->current_action();

        switch ( $action ) {

            case 'delete':
                self::delete_activity( absint( $_GET['activity'] ) );

                wp_redirect( esc_url( add_query_arg() ) );

                break;

            case 'bulk-delete':
                $ids = esc_sql( $_POST['bulk-selected'] );
                foreach ( $ids as $id ) {
                    self::delete_activity( $id );
                }

                wp_redirect( esc_url( add_query_arg() ) );

                break;

            case 'bulk-export':
                $ids = esc_sql( $_POST['bulk-selected'] );

                self::export_activities( $ids );

                wp_redirect( esc_url( add_query_arg() ) );
                break;

            default:
                // do nothing or something else
                return;
                break;
        }

        return;

    }

I hope this helps you ;)

  • Thank you @Oterox , I had done the same though :) – Codeformer Sep 3 '15 at 2:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.