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

发布于 2024-08-17 18:58:14 字数 1108 浏览 5 评论 0 原文

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

要求

  • 只有具有管理员角色的用户
  • 创建/编辑额外的用户配置文件字段
  • 才能为所有其他用户

我知道如何在 WordPress 中添加额外的用户配置文件字段(请参阅下面的链接),但我不知道如何限制编辑该字段给定角色中的用户以及具有管理员角色的用户如何为所有用户创建/编辑此字段。在我看来,我必须在 WordPress 仪表板/用户/新用户和/或仪表板/用户/作者和/或仪表板/用户/作者下添加新代码。用户。

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

在我看来,添加和使用自定义用户配置文件字段几乎是我想做的,但我仍然不知道如何管理“只有管理员”可以做到这一点。

I'm developing a custom plugin in PHP so existing plugins are not usable. What I want to achieve is that I want to display different url within a post for some users . For users that are registered in wordpress, contacted me and are 'approved'. I want to set up this extra user profile field so I can use this field in a condition. So guests and users without this field or without the right value in this field will get url1 but the other ones url2.

requirements

  • only users with admin role
  • can create/edit extra user profile field
  • for all other users

I know how I can add extra user profile field in Wordpress (see the links below), but I don't know how to restrict editing that field to users in a given role and how user with admin role can create/edit this filed for all users. It seems to me that I have to add new code under wordpress dashboard/users/new user and/or dashboard/users/authors & users.

I did some google search and found few sites on how to add extra user profile field

It seems to me that Adding and using custom user profile fields is almost what I want to do but I still do not know how to manage the "only admin' can do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疾风者 2024-08-24 18:58:14

怎么样:

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

请参阅此链接,了解 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" /><br />
                <span class="description">Please enter your Twitter username.</span>
            </td>
        </tr>

    </table>
<?php }

How about this:

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

see this link about user capabilities for wordpress

So with the above, and that link you gave, maybe something like this:

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" /><br />
                <span class="description">Please enter your Twitter username.</span>
            </td>
        </tr>

    </table>
<?php }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文