首页 文章

用户权限 - 触发用户角色代码(Wordpress)

提问于
浏览
3

我正在用PHP开发一个自定义插件,因此现有的插件不可用 . 我想要实现的是,我希望在帖子中为某些用户显示不同的URL . 对于在wordpress中注册的用户,请与我联系并获得“批准” . 我想设置这个额外的用户配置文件字段,以便我可以在条件中使用此字段 . 因此,没有此字段或在此字段中没有正确值的访客和用户将获得url1,但其他url2 .

要求

  • 仅限具有管理员角色的用户

  • 可以创建/编辑额外的用户配置文件字段

  • 适用于所有其他用户

我知道如何在Wordpress中添加额外的用户 Profiles 字段(请参阅下面的链接),但我不知道如何限制将该字段编辑给给定角色的用户以及管理员角色的用户如何创建/编辑此字段所有用户 . 在我看来,我必须在wordpress dashboard / users / new user和/或dashboard / users / authors&users下添加新代码 .

我做了一些谷歌搜索,发现很少有关于如何添加额外用户 Profiles 字段的网站

在我看来,Adding and using custom user profile fields几乎是我想做的,但我仍然不知道如何管理“唯一的管理员”可以做到这一点 .

1 回答

  • 3

    这个怎么样:

    $ID="2";
    $user = new WP_User($ID);
    if ($user->wp_capabilities['administrator']==1) {
        //code here
    }
    

    看到这个关于user capabilities for wordpress的链接

    所以,通过以上,以及您给出的链接,可能是这样的:

    function my_show_extra_profile_fields( $user ) {
        if ($user->wp_capabilities['administrator']!=1) {
            return false;
        } ?>
        <h3>Extra profile information</h3>
    
        <table class="form-table">
    
            <tr>
                <th><label for="twitter">Twitter</label></th>
    
                <td>
                    <input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" />
    <span class="description">Please enter your Twitter username.</span> </td> </tr> </table> <?php }

相关问题