views:

725

answers:

1

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.

+3  A: 

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 }
@jolierouge: I updated the question. I think it was not so clear so everybody could understand what I wanted to code. I will have a look into your answer.Thank you for that.
Radek
Hey guy, well I can't code the whole thing for you, but it looks like the meat of your problem here is kind of solved. At least, how to check if they are an admin.Maybe you should open a new question specific to another solution, i.e. don't just edit this question to every new one that comes up, leave it here for others looking for a similar solution.
@jolierouge: All my requirements were in the original post. Maybe it was not so clear. For various reasons my original post was edited and by somebody else and it seems to me that it created even more confusion. To be honest I do not know what to do. Very important part of the solution to my question is WHERE I use the piece of the code. It needs to be used when ADMIN is editing user profile.
Radek