ani*_*wal 3 wordpress custom-post-type
我正在开发一个 WordPress 项目,我想在我的自定义帖子上添加批量操作。
我已经使用Custom Post Type
UI 插件进行自定义帖子和Advanced Custom Fields
用于自定义字段的插件。
请建议我任何代码或插件来为我的自定义帖子添加批量操作。
谢谢,阿尼凯特。
小智 5
自 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 );
}
}
Run Code Online (Sandbox Code Playgroud)
注意1:定义对象
bulk_actions
时必须使用过滤器。这就是我在第2行中使用操作的原因。WP_Screen
current_screen
注意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/
归档时间: |
|
查看次数: |
4326 次 |
最近记录: |