Wordpress自定义批量操作处理程序未触发

时间:2018-09-01 20:40:10

标签: php wordpress custom-wordpress-pages

我对Wordpress和PHP还是陌生的,正在尝试将自定义字段添加到自定义帖子的批量编辑列表中。我关注了以下博客文章,但我的handle_bulk_actions回调未触发: https://make.wordpress.org/core/2016/10/04/custom-bulk-actions/

这是我的代码:

/**
 * 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' );

谢谢!

1 个答案:

答案 0 :(得分:0)

我知道这个答案已经晚了几个月,但是今天我遇到了这个问题,直到我意识到在尝试进行测试时,实际上并没有任何帖子被检查过。如果这是您的问题,我想您已经找到了答案,但是如果其他任何人都遇到了这种情况,并且他们偶然发现了这个问题以寻求解决方案(如我所知),这就是您的问题所在。 / p>