我已经能够防止基于用户ID和页面ID删除和编辑帖子,但是,我需要限制基于自定义字段的编辑和删除权限。
不知道该怎么做。
Current working code for restricting user ID from editing/deleting based on page ID:
function restrict_post_deletion($post_ID){
$user = get_current_user_id();
$restricted_users = array(1,2,3,4,5,6,25,54,19);
$restricted_pages = array(2,21,52,64);
if(in_array($user, $restricted_users) && in_array($post_ID, $restricted_pages)){
do_action(\'admin_page_access_denied\');
wp_die( __(\'You cannot modify or delete this entry.\') );
}
}
add_action(\'edit_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'before_edit_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'wp_trash_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'before_delete_post\', \'restrict_post_deletion\', 10, 1);
所以我们说自定义字段是:$customstatus
$customstatus已通过一个下拉列表在帖子中定义,其中有两个选项“可用”和“受保护”。
在本例中,restricted\\u页面和post\\u ID不再相关,因为我没有基于自定义字段进行限制。
我希望restrict\\u post\\u deletion功能可以防止根据用户ID和$customstatus==“protected”编辑/删除帖子。
The following is not working:
function restrict_post_deletion($post_ID){
$user = get_current_user_id();
$customstatus = get_option(\'wp_customstatus\');
$restricted_users = array(1,2,3,4,5,6,25,54,19);
if(in_array($user, $restricted_users) && $customstatus == \'protected\'){
do_action(\'admin_page_access_denied\');
wp_die( __(\'You cannot modify or delete this entry.\') );
}
}
add_action(\'edit_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'before_edit_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'wp_trash_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'before_delete_post\', \'restrict_post_deletion\', 10, 1);
我哪里做错了?非常感谢您的帮助。
SO网友:Nima Moradi
通过更多的研究和一些尝试和错误,我找到了正确的代码,可以根据用户ID和自定义元字段限制帖子的编辑。
$user = get_current_user_id();
$super_users = array(1);
函数中的前两段代码对于限制用户根据其用户ID编辑/删除非常重要。
1
作为主要管理员。您始终可以根据ID扩展阵列以包括其他用户。
提供访问和阻止其他所有人比阻止某些用户更容易。这样,新用户就不会意外地获得编辑/删除特定帖子的权限。
global $post;
为了从自定义字段中获取信息,需要定义$post,并使用以下代码从数组中获取字段。
$customstatus = get_post_meta( $post->ID, \'protected_value\', true );
我调整了
if
语句,而不是限制某些用户,它只允许
$super_users
使用以下代码:
if(!in_array($user, $super_users) && $customstatus == \'Protected\'){
do_action(\'admin_page_access_denied\');
wp_die( __(\'You cannot modify or delete this entry.\') );
exit;
}
代码将返回
admin_page_access_denied
有一行文字说明帖子不能编辑或丢弃。然后添加以下操作
restrict_post_deletion
影响。
add_action(\'edit_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'wp_trash_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'before_delete_post\', \'restrict_post_deletion\', 10, 1);
Here is the full code that goes into your themes functions.php
function restrict_post_deletion(){
$user = get_current_user_id();
$super_users = array(1);
global $post;
$customstatus = get_post_meta( $post->ID, \'protected_value\', true );
if(!in_array($user, $super_users) && $customstatus == \'Protected\'){
do_action(\'admin_page_access_denied\');
wp_die( __(\'You cannot modify or delete this entry.\') );
exit;
}
}
add_action(\'edit_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'wp_trash_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'before_delete_post\', \'restrict_post_deletion\', 10, 1);