3

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.

Radek
  • 13,813
  • 52
  • 161
  • 255
  • Have you done any code to get you started? Or do you want the code complete? – random Jan 19 '10 at 08:16
  • @Crises of Identity: I updated my question. I do not have any code so far.I am happy if anybody gives me either any hint or the complete code. I will learn both ways as I need to understand the code in the end. – Radek Jan 19 '10 at 09:27
  • 1
    Why do you think there should be a way to add an extra user profile field without being an admin? It sounds like an admin function to me, and that you're looking for a security breach, which we generally don't help with. Also, what sort of programming question are you asking; right now, this looks like a job for SuperUser if anything. – David Thornley Jan 19 '10 at 14:37
  • @David Thornley: I think it's exactly the opposite of what you're saying. He wants *only* admins to be able to access this field. – Michael Myers Jan 19 '10 at 16:05
  • @David Thornley: what is a security breach about adding a phone number to your wordpress profile if you want to be displayed to everybody? Using the link in my questions I can implement it. My question how can code that only admin can add and edit that extra field. In my opinion is not related to security at all. Only if coded bad way. Maybe you can tell us more why it would be a security breach. I am happy to change my opinion. – Radek Jan 19 '10 at 18:13
  • @mmyers, Radek: I reread the question more carefully, and your comment, Radek, and saw my misunderstanding. I have edited the question to clarify your second sentence, since if I misunderstood it other people likely will. My apologies. – David Thornley Jan 19 '10 at 18:34
  • 1
    @David Thornley: And now I see the usefulness of the comment notification system, too. – Michael Myers Jan 19 '10 at 18:51
  • @pib: my last edit was only adding one ore word to by really exact with the question. I have a feeling that it might be about my comment towards David? I really do not know. I am happy to learn ... – Radek Jan 19 '10 at 19:38
  • @John Rudy: thank you for the vote and thank you for showing me how to vote to reopen closed question :-) I was about to search for it... – Radek Jan 19 '10 at 22:12
  • 1
    Vote to reopen. Also edited to explicitly state the problem in a programming context, and the clarify the exact issue (ie, it's about making code that only works for admins) – Adam Davis Jan 21 '10 at 16:00
  • @Adam Davis: thank you for 1)voting 2) rewriting my question and the most 3) for your comment where you nicely explained that you also edited my question and why you did so. This is just great approach.I love that. – Radek Jan 21 '10 at 18:40

1 Answers1

3

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 }
J. Martin
  • 1,683
  • 2
  • 17
  • 33
  • @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 Jan 21 '10 at 20:03
  • 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. – J. Martin Jan 22 '10 at 13:18
  • @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 Jan 22 '10 at 18:15