我尝试使用S2Member的角色设置课程创建限制。例如,对于第一级,用户只能创建2个课程和2个单元。我添加了这段代码,但它仅适用于后端。在前端创建(使用wplms主题)中,用户仍然可以创建新的课程和单元。我不是编码方面的专家,只是想找到解决这个问题的方法。有什么建议吗?
add_action( 'admin_head-post-new.php', 'check_post_limit' );
function check_post_limit() {
global $userdata;
global $post_type;
global $wpdb;
if( $post_type === 'course' ) {
$item_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'course' AND post_author = $userdata->ID" );
if(( $item_count >= 2 ) && (current_user_is("s2member_level1")) ) { wp_die( "Do not create too much courses." ); }
} elseif( $post_type === 'unit' ) {
$shopinfo_count = $wpdb->get_var( "SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'unit' AND post_author = $userdata->ID" );
if(( $item_count >= 2 ) && (current_user_is("s2member_level1")) ) { wp_die( "Do not create too much units." ); }
}
return;
}