背景:
我尝试了https://wordpress.org/plugins/bainternet-posts-creation-limits和https://wordpress.org/plugins/limit-posts/,但由于某种原因它们不适用于我的自定义帖子。因此,我决定编写自己的 php 脚本,并将其放入子主题的 function.php 中,以限制某些用户可以创建的某些自定义帖子类型的数量。
结构:
我使用的是安装在 bluehost 上的 wordpress 5.4.2。我正在使用 geodirectory 来创建自定义帖子类型(从业者和组织)和 memberpress 来管理权限。
约束:
因为我网站上的另一个插件只授予特定角色的权限,所以我给不是管理员的每个人该特定用户角色,因此不能按用户角色限制发布权限。这就是为什么我使用会员资格来管理出版能力。
问题/问题:
运行代码后:
1-所有用户,而不仅仅是会员资格不足的非管理员,不能保存新的 CPT。即使是管理员也无法保存 CPT。
2- 用户在单击提交时收到此消息:“您没有执行此操作的权限。” 我写了一条不同的消息,以发送给此操作的会员资格错误的用户。
方法:
//Before a newly created post is saved to the database, filter by membership
add_filter( 'wp_insert_post_data' , 'tml_filter_by_membership' , 99, 2 );
/**
99 refers to the priority when this filter is executed; 99 indicates that it is one of the last filters executed on this action
2 indicates that we want both arguments given by the filter (because the second one contains sanitized but unmodified data--which is the data we want). Without 2, the function defaults to 1, giving us only slashed post data
**/
//filter by membership only acts on non-admin posts of certain post types
function tml_filter_by_membership( $data , $postarr ) {
//take only non-admin posts
if (! user_can($postarr['post_author'], 'manage_options')) {
// continue only if the post type is gd_practitioner or gd_studio
if ($postarr['post_type'] == 'gd_practitioner' || $postarr['post_type'] == 'gd_organization'){
//Set membership post limits by post_type
$membership_post_limits = tml_set_membership_post_limits_by_post_type();
//Get author's membership
$author_memberships = tml_get_post_author_memberships($postarr);
//Look up the author's post limit for that post type within the membership post limits array
$author_post_limit = tml_get_author_post_limit($author_memberships, $membership_post_limits, $postarr);
//Count author's published posts of the particular post type
$number_of_published_posts_by_author = tml_get_author_published_posts($postarr);
//Compare number of published posts to author's post limit and return $postarr
tml_compare_posts_to_limit($author_post_limit, $number_of_published_posts_by_author, $postarr);
}
}
}
//set membership post limits by post_type
function tml_set_membership_post_limits_by_post_type(){
$membership_post_limits=Array(
462 => Array ('gd_practitioner' =>1, 'gd_organization' =>1),
463 => Array ('gd_practitioner' =>1, 'gd_organization' =>1),
464 => Array ('gd_practitioner' =>1, 'gd_organization' =>1),
465 => Array ('gd_practitioner' =>10, 'gd_organization' =>10),
466 => Array ('gd_practitioner' =>1, 'gd_organization' =>1),
467 => Array ('gd_practitioner' =>10, 'gd_organization' =>10),
752 => Array ('gd_practitioner' =>1, 'gd_organization' =>1),
753 => Array ('gd_practitioner' =>1, 'gd_organization' =>1),
754 => Array ('gd_practitioner' =>1, 'gd_organization' =>1)
);
return $membership_post_limits;
}
//Get author's membership
function tml_get_post_author_memberships($postarr){
if( class_exists('MeprUser') ){
$user_id = $postarr['post_author'];
$user = new MeprUser( $user_id);
$get_memberships = $user->active_product_subscriptions();
$user_memberships = array_values( array_unique( $get_memberships ) );
return $author_memberships;
} else {
return false;
}
}
//Look up the author's post limit for that post type within the membership post limits array, using $postarr to give the post type, and $user_memberships to give the author's memberships
function tml_get_author_post_limit($author_memberships, $membership_post_limits, $postarr){
$author_post_limit=1;
Foreach ($author_memberships as $membership){
if ($author_post_limit < $membership_post_limits[$membership][$postarr['post_type']]){
$author_post_limit=$membership_post_limits[$membership][$postarr['post_type']];
}
}
return $author_post_limit;
}
//Count author's published posts of the particular post type
function tml_get_author_published_posts($postarr){
$posts = get_posts( array(
'author' => $postarr['post_author'],
'post_type '=> $postarr['post_type'],
'numberposts '=> -1,
'post_status '=> 'publish'
));
$number_of_published_posts_by_author=count($posts);
return $number_of_published_posts_by_author;
}
/**
//Another option, using count_user_posts function instead
function tml_get_author_published_posts($postarr){
$author= $postarr['post_author'];
$post_type = $postarr['post_type'];
$number_of_published_posts_by_author=count_user_posts($author, $post_type);
return $number_of_published_posts_by_author;
}
**/
//Compare number of published posts to author's post limit
function tml_compare_posts_to_limit($author_post_limit, $number_of_published_posts_by_author, $postarr){
$message="";
if ( $number_of_published_posts_by_author>= $author_post_limit){
$postarr['post_status']='draft';
$message="You have exceeded the number of listings for your membership";
}
echo "<div> ${message} </div>";
return $postarr;
}