当前位置:奉上博客 » 站长之家 » 正文

B2主题教程-在文章列表左上角添加一个批量设置下载权限的选项

在文章列表左上角添加一个批量设置下载权限的选项

点击选择好需要设置的文章,然后选择该选项,点应用即可批量设置下载权限

将一下代码复制到主题functions.php最下方即可

add_filter( 'bulk_actions-edit-post', function($bulk_actions) {
$bulk_actions['download_permission'] = __( '下载权限批量设置', 'download_permission');
return $bulk_actions;
});
add_filter( 'handle_bulk_actions-edit-post', function( $redirect_to, $doaction, $post_ids ) {
$setting = 'all|free
lv1|free
lv2|comment
lv3|login
lv4|money=10
lv4|credit=30';//需要设置的对应下载权限,自行修改
 
 
if ( $doaction !== 'download_permission' ) {
return $redirect_to;
}
foreach ( $post_ids as $post_id ) {
$meta = get_post_meta($post_id,'b2_single_post_download_group',true) ? get_post_meta($post_id,'b2_single_post_download_group',true) : array();
foreach($meta as $k => $v){
$meta[$k]['rights'] = $setting;
}
update_post_meta($post_id,'b2_single_post_download_group',$meta);
}
$redirect_to = add_query_arg( 'download_setting', count( $post_ids ), $redirect_to );
return $redirect_to;
}, 10, 3 );
add_action( 'admin_notices', function() {
if ( ! empty( $_REQUEST['download_setting'] ) ) {
echo '设置成功';
}
} );

相关文章