slotdp02
slotdp02

Reputation: 438

Wordpress Custom Bulk Actions handler not firing

I'm pretty new to Wordpress and PHP and am trying to add a custom field to a bulk edit list for a custom post. I've followed the following blog post but my handle_bulk_actions callback is not firing: https://make.wordpress.org/core/2016/10/04/custom-bulk-actions/

Here's my code:

/**
 * Adds a new item into the Bulk Actions dropdown for custom products 
list.
 */
function register_my_bulk_actions( $bulk_actions ) {
    $bulk_actions['out_of_stock'] = 'Mark as Out of Stock';
    $bulk_actions['in_stock'] = 'Mark as In Stock';
    debug_to_console( 'Hello console' );
    return $bulk_actions;
}
add_filter( 'bulk_actions-edit-custom_product', 'register_my_bulk_actions' );

/**
 * Handles the bulk action above.
 * NOT FIRING!!
 */
function my_bulk_action_handler( $redirect_to, $action, $post_ids ) {
    debug_to_console( 'Running handler' );
    if ( $action !== 'out_of_stock' || $action !== 'in_stock') {
        return $redirect_to;
    }

    // let's remove query args first
    $redirect_to = remove_query_arg( array( 'out_of_stock_done', 'in_stock_done' ), $redirect );

    foreach ( $post_ids as $post_id ) {
        if ($action === 'out_of_stock ') {
            wp_update_post( array(
                'ID' => $post_id,
                'in_stock' => 'no',
            ) );
            $redirect_to = add_query_arg( 'out_of_stock_done', count( $post_ids ), $redirect_to );
        }
        if ($action === 'in_stock ') {
            wp_update_post( array(
                'ID' => $post_id,
                'in_stock' => 'yes',
            ) );
            $redirect_to = add_query_arg( 'in_stock_done', count( $post_ids ), $redirect_to );
        }
    }

    return $redirect_to;
}
add_filter( 'handle_bulk_actions-edit-custom_product', 'my_bulk_action_handler', 10, 3 );

/**


* Shows a notice in the admin once the bulk action is completed.
 */
function my_bulk_action_admin_notice() {
    debug_to_console( 'Running notifier' );
    if ( ! empty( $_REQUEST['bulk_out_of_stock'] ) ) {
        $success_oos = intval( $_REQUEST['bulk_out_of_stock'] );

        printf(
            '<div id="message" class="updated fade">' .
            _n( '%s product updated!', '%s products updated!', $drafts_count, 'domain' )
            . '</div>',
            $success_oos
        );
    }
}
add_action( 'admin_notices', 'my_bulk_action_admin_notice' );

THANKS!

Upvotes: 0

Views: 962

Answers (1)

Christ Wilhelm
Christ Wilhelm

Reputation: 31

I know this answer is a few months late, but I was having this issue today, until I realized that I didn't actually have any posts checked off when I was trying to test it. If this was your issue, I imagine you've figured it out by now, but in case this happens to anyone else and they stumble upon this question looking for a solution (as I have), here is where your problem may lie.

Upvotes: 1

Related Questions