Question

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.

Was it helpful?

Solution

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 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top