Php 限制Wordpress中非管理员的某些标记

Php 限制Wordpress中非管理员的某些标记,php,wordpress,tags,privileges,user-roles,Php,Wordpress,Tags,Privileges,User Roles,我经营一个多作者网站。我想限制我的作者选择一些标记 到目前为止,我发现的唯一选项是用列表(类似于类别列表)替换自由标记文本字段的技术。这对我来说不是一个解决方案,因为我需要我的作者能够创建新的标签 肯定有办法限制非管理员的特定标记?你知道怎么做吗?欢迎任何头脑风暴或适当的解决方案。如果作者不是您,您可以创建一个简单的插件,在保存文章时删除指定的标签。 使用add\u操作保存帖子时,可以调用函数(“保存帖子”、“删除标签”函数),10,2 然后创建一个函数,如 function remove_ta

我经营一个多作者网站。我想限制我的作者选择一些标记

到目前为止,我发现的唯一选项是用列表(类似于类别列表)替换自由标记文本字段的技术。这对我来说不是一个解决方案,因为我需要我的作者能够创建新的标签


肯定有办法限制非管理员的特定标记?你知道怎么做吗?欢迎任何头脑风暴或适当的解决方案。

如果作者不是您,您可以创建一个简单的插件,在保存文章时删除指定的标签。 使用
add\u操作保存帖子时,可以调用函数(“保存帖子”、“删除标签”函数),10,2

然后创建一个函数,如

function remove_tags_function($postID, $post){
if($parent_id = wp_is_post_revision($postID))
    {
    $postID = $parent_id;
    $post = get_post($postID);
    }
if($post->post_author != 'YOUR AUTHOR NUMBER'){
$tags = wp_get_post_tags( $post_id );
$i = 0;
foreach($tags as $tag){
 if(in_array("TAG NAME", $tag)) unset $tags[$i];
 $i++;
}

}}
我还没有测试过它,但从逻辑上讲它会工作的

编辑:

那不行

尝试将以下内容放入插件文件夹中的.php文件中。目前还没有测试,但看起来不错

<?php
add_action('save_post', 'remove_tags_function',10,2);
        function remove_tags_function($postID, $post){
        if($parent_id = wp_is_post_revision($postID))
            {
            $postID = $parent_id;
            $post = get_post($postID);
            }
        $user_info = $user_info = get_userdata($post->post_author);
        if($user_info->user_level != 10){ // 10 = admin
        untag_post($postID, array('TAGS', ... ));

        }}
        function untag_post($post_ids, $tags) {
        global $wpdb;
        if(! is_array($post_ids) ) {
            $post_ids = array($post_ids);
        }
        if(! is_array($tags) ) {
            $tags = array($tags);
        }

        foreach($post_ids as $post_id) {
            $terms = wp_get_object_terms($post_id, 'post_tag');
            $newterms = array();
            foreach($terms as $term) {
                if ( !in_array($term->name,$tags) ) { //$term will be a wordpress Term object.
                    $newterms[] = $term;
                }
            }
            wp_set_object_terms($post_id, $newterms, 'post_tag', FALSE);
        }
    } // I got this from http://wordpress.stackexchange.com/questions/49248/remove-tags-from-posts-in-php/49256#49256
?>

我认为理想的解决方案是有条件地过滤
post_tags\u meta_box
中使用的分类查询,因为它提供了建议,如果有人试图手动键入您不希望他们使用的标记,甚至可能会提供一些错误处理,但我不知道有什么过滤器可以帮助实现这一点

扩展Giordano的建议和引用,您可以在functions.php中使用类似的内容

add_action('save_post', 'remove_tags_function', 10, 1); //whenever a post is saved, run the below function

function remove_tags_function( $post_id ){
    if(!current_user_can('manage_options')){ // if the logged in user cannot manage options (only admin can)
        $post_tags = wp_get_post_terms( $post_id, 'post_tag', array( 'fields'=>'names' ) ); //grab all assigned post tags
        $pos = array_search( 'tag-to-be-deleted', $post_tags ); //check for the prohibited tag
        if( false !== $pos ) { //if found
            unset( $post_tags[$pos] ); //unset the tag
            wp_set_post_terms ($post_id, $post_tags, 'post_tag'); //override the posts tags with all prior tags, excluding the tag we just unset
        }
    }//end if. If the current user CAN manage options, the above lines will be skipped, and the tag will remain
}
非管理员用户仍可以键入并添加要删除的标签,但不会粘贴到帖子上。保存后,标签将被剥离。如果用户真的很专注,他们可以用不同的方式拼写,或者,正如你所看到的,改变大小写,但是无论他们做什么,从技术上讲,它都不会是同一个标签,你将能够为你需要的任何主题目的保持它的纯粹性。我无法想象一个没有管理能力的用户可以添加禁止标签的情况,但我知道最好不要说不


如果希望允许某些非管理员用户将禁止标记分配给帖子,则必须修改传递到第4行
If(!current_user_can(“…”)中的参数{
。有关您可以传递到此条件语句的其他功能的信息,请检查。检查功能比检查角色容易得多,因此选择一个逻辑功能,该功能仅限于您希望免于删除标记的用户级别。

基于crowjonah的精彩回答,下面是一个需要重新定义的功能移动多个标记:

<?php           
/** block non-admins from using specific post tags **/
function remove_tags_function( $post_id ){
    if(!current_user_can('manage_options')){ // if the logged in user cannot manage options (only admin can)
        $post_tags = wp_get_post_terms( $post_id, 'post_tag', array( 'fields'=>'names' ) ); //grab all assigned post tags
        $pos = array_intersect( array('TAG1', 'TAG2', 'TAG3', 'ETC...'), $post_tags ); //check for the prohibited tag
        if( !empty($pos) ) { //if found
            $post_tags = array_diff($post_tags, $pos);
            wp_set_post_terms ($post_id, $post_tags, 'post_tag'); //override the posts tags with all prior tags, excluding the tag we just unset
        }
    }//end if. If the current user CAN manage options, the above lines will be skipped, and the tag will remain
}
add_action('save_post', 'remove_tags_function', 10, 1); //whenever a post is saved, run the below function      
?>


Itamar

这是一个非常有趣的方法。您是否有可能详细说明您的答案?可能有一个工作代码示例,我可以测试运行它,看看它是否正常工作?是否有可能将其添加到function.php文件中,并以这种方式工作?您需要创建一个.php文件并将其放入插件中文件夹。现在我想了想,你不能只是取消设置标签,你需要用一个特定的函数来删除它。我今晚晚些时候会研究它。非常感谢。显然我非常需要解决这个问题。如果你今晚晚些时候有机会解决这个问题,请更新这个答案。我不介意通过通过paypal支付几美元,感谢您的努力。感谢您的更新,但是,我测试了它,没有结果-很可能是因为我没有正确执行测试。我将您的代码复制到我的插件文件夹,在untag_post中选择特定的标记($postID,array('TAGS'),…);然后用一个非管理员帐户对其进行了测试。它被标记。但是,我很不确定该插件是否处于活动状态。我在wp admin的插件列表中找不到该插件。根据我使用插件的经验,您需要激活它们,对吗?您是否有可能自己对代码进行测试并发布结果?我添加了此代码在my function.php中,将要删除的标记替换为我想要的受限标记,但它不起作用。当使用作者级别的用户登录时,它仍然出现在列表中。可能您的代码中有输入错误?有其他解决方案吗?您应该在以非管理员用户身份登录时执行新的发布周期。尝试添加标记并保存post,然后刷新以查看标签是否粘贴。我按照你的建议做了,但没有成功。我与作者级别的用户创建了一个新帖子,添加了标签,保存了帖子。标签仍然存在。通过注销并以管理员身份登录进行双重检查,它确实存在。这对你有用吗?有进一步的想法吗?感谢你的帮助s、 很抱歉,
是\u admin
条件,我使用它来检查仪表板是否正在显示,在添加标记时,通常会返回true。我已将其更改为
当前用户\u can('manage\u options')
,应该可以!它仍然不起作用。我添加了您的更新代码,并按照前面列出的相同步骤进行了新的测试,标记仍然存在。您的代码中是否有其他问题?