繁体   English   中英

自定义帖子类型的批量操作

[英]Bulk action for custom post types

我正在处理一个 wordpress 项目,我想在我的自定义帖子上添加批量操作。

我已经将Custom Post Type UI 插件用于自定义帖子和Advanced Custom Fields插件用于自定义字段。

请建议我为我的自定义帖子添加批量操作的任何代码或插件。

谢谢,阿尼凯特。

从 WordPress 4.7(2016 年 12 月发布)开始,可以在不使用 JavaScript 的情况下添加自定义批量操作。

//Hooks
add_action( 'current_screen', 'my_bulk_hooks' );
function my_bulk_hooks() {
    if( current_user_can( 'administrator' ) ) {
      add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions' );
      add_filter( 'handle_bulk_actions-edit-post', 'my_bulk_action_handler', 10, 3 );
      add_action( 'admin_notices', 'my_bulk_action_admin_notice' );      
    }
}   

//Register
function register_my_bulk_actions($bulk_actions) {
  $bulk_actions['email_to_eric'] = __( 'Email to Eric', 'text_domain');
  return $bulk_actions;
}

//Handle 
function my_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
  if ( $doaction !== 'email_to_eric' ) {
    return $redirect_to;
  }
  foreach ( $post_ids as $post_id ) {
    // Perform action for each post.
  }
  $redirect_to = add_query_arg( 'bulk_emailed_posts', count( $post_ids ), $redirect_to );
  return $redirect_to;
}

//Notices
function my_bulk_action_admin_notice() {
  if ( ! empty( $_REQUEST['bulk_emailed_posts'] ) ) {
    $emailed_count = intval( $_REQUEST['bulk_emailed_posts'] );
    printf( '<div id="message" class="updated fade">' .
      _n( 'Emailed %s post to Eric.',
        'Emailed %s posts to Eric.',
        $emailed_count,
        'text_domain'
      ) . '</div>', $emailed_count );
  }
}

bulk_actions bulk_actions WP_Screen对象时必须使用bulk_actions过滤器。 WP_Screen就是我在第 2 行中使用current_screen操作的原因。

Note.2:如果您想向 woocommerce 产品页面等自定义页面添加批量操作,只需更改第 5 行和第 6 行中的屏幕 ID。例如:
add_filter( 'bulk_actions-edit-product', 'register_my_bulk_actions' );
add_filter( 'handle_bulk_actions-edit-product', 'my_bulk_action_handler', 10, 3 );

更多信息 :

使用自定义批量操作

https://make.wordpress.org/core/2016/10/04/custom-bulk-actions/

使用 WordPress 功能的“register_post_type”,比额外的插件更容易参考: https : //codex.wordpress.org/Function_Reference/register_post_type

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM