0

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!

1 Answer 1

0

Hi first fix your condition

if ( $action !== 'out_of_stock' || $action !== 'in_stock') {

this condition will always run because your action is in contrast with "out_of_stock" or "in_stock". So I think if you change the condition from OR to AND it will work.

Then find out if the screen id is the one your looking for, and then modify your filter: to understand what is your screen id, add this to your plugin or your theme function.php, and remember to remove it later:

add_filter( 'wc_product_has_unique_sku', '__return_false' );

add_action('init', 'test_function');
function test_function(){
    $value = get_current_screen();
    echo '<pre>';
    print_r($value);
    echo '</pre>';
}

then when you get the correct screen id, for instance SCREEN_ID, modify your filter like this:

add_filter( 'bulk_actions-SCREEN_ID', 'register_my_bulk_actions' ); 
5
  • Good catch Arash, I appreciate it, but the debug function before that conditional: debug_to_console( 'Running handler' ); is not even running.
    – slotdp02
    Commented Sep 4, 2018 at 3:31
  • okay, it is possible that your filter doesn't fire, so what is the screen id of the page you want to do something on it. First find that and then rewrite your filter: add_filter( 'bulk_actions-SCREEN_ID', 'register_my_bulk_actions' ); suppose that the screen id is: SCREEN_ID Commented Sep 4, 2018 at 6:10
  • get the screen_id using: get_current_screen(); and see what is it, then modified your filter. I personally do something like this:add_filter( 'wc_product_has_unique_sku', '__return_false' ); add_action('init', 'test_function'); function test_function(){ $value = get_current_screen(); echo '<pre>'; print_r($value); echo '</pre>'; } Commented Sep 4, 2018 at 6:15
  • Hmm, that filter and action cause an error. Would i need to do that though if i know what the screen id is? In my example above, the bulk actions are successfully registered with edit-custom_product. Wouldn't that mean that that is the screen id?
    – slotdp02
    Commented Sep 6, 2018 at 5:04
  • Thank you! if ( $action !== 'out_of_stock' || $action !== 'in_stock') that conditional was a problem, even with the and in there. Removing it made the function fire.
    – slotdp02
    Commented Sep 7, 2018 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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