10

How do I create a new usermeta field with a drop down selection values?

Im want to create a conditional statement for all users with a certain value of the new custom field I want.

For example,

The new field would be: Approved The drop down values are: Yes and No

The conditional statement will recognize all users with the Approved field value of Yes. Then it will post a code.

Im working with the wp_get_current_user function(), which does exactly what I need, but I just need a new custom usermeta field. In the example the new usermeta field would be "artwork_approved."

Example:

wp_get_current_user();
if ($current_user->artwork_approved == 'Yes'){
    echo 'Thank you for approving your artwork!';
}

There seems to be no plugin for this and I really need this feature. I would really appreciate some advice on creating a new usermeta with drop down options.

*UPDATE:

I used the Register Plus Redux to create a new usermeta field, called "Artwork Approved." I made it a drop down option, with options "No" and "Yes." The No option is set as default.

This created the "Artwork Approved" usermeta field. I manage user accounts and choose either Yes or No. Now with this new usermeta field, Im using a function that should check if the current user has the Artwork Approved with value of Yes. Then it is supposed show a certain code.

Here is the if statement Im using with the new usermeta field:

<?php global $current_user; get_currentuserinfo(); if ($current_user->artwork_approved == 'Yes') { ?>

echo 'Your artwork is approved';

<?php } else { ?>         

echo 'Your artwork is not approved';

<?php } ?> 

But what's happening is it's not recognizing the first part of the if statement. If I log into any account with the artwork approved, the if statement only shows the "else" part even if I have the option "Yes" for Artwork Approved.

I don't know why it isn't recognizing the Yes Option as I have it in the statement.

Thanks

2 Answers 2

23

You can create a simple plugin to hook into the user profile actions and add a new field.

To add the field to the form you can hook into the show_user_profile and edit_user_profile actions and output the form field HTML. The below uses a checkbox rather than a drop-down.

add_action('show_user_profile', 'my_user_profile_edit_action');
add_action('edit_user_profile', 'my_user_profile_edit_action');
function my_user_profile_edit_action($user) {
  $checked = (isset($user->artwork_approved) && $user->artwork_approved) ? ' checked="checked"' : '';
?>
  <h3>Other</h3>
  <label for="artwork_approved">
    <input name="artwork_approved" type="checkbox" id="artwork_approved" value="1"<?php echo $checked; ?>>
    Artwork approved
  </label>
<?php 
}

Then you need to hook into the personal_options_update and edit_user_profile_update actions, get the value of your field and save this as user meta.

add_action('personal_options_update', 'my_user_profile_update_action');
add_action('edit_user_profile_update', 'my_user_profile_update_action');
function my_user_profile_update_action($user_id) {
  update_user_meta($user_id, 'artwork_approved', isset($_POST['artwork_approved']));
}

Your condition would then be as below.

if (get_user_meta($current_user->ID, 'artwork_approved', true)) {
4
  • Thanks Richard. I found a solution that is somewhat working... Im using the Register Plus Redux plugin, which can create the new usermeta. I have updated my post with more information. I would appreciate your input. Thanks a lot!
    – gdinari
    Commented May 27, 2011 at 13:38
  • Thanks Richard your solution worked like a charm. The only problem I see is that when I do check the box for the user approval in the admin section, after updating the page the box is not checked. If one of our staff looks at that client they wouldn't know if they're approved or not. So if I wanted to uncheck (unapprove) the box how would I do that? Thank you
    – gdinari
    Commented May 31, 2011 at 16:12
  • I guess I need to create this as a plugin like you suggested...I added the first block of code that you gave me to my admin members php page, where I upload the photo for approval. The second and last block I added to the front to front end of the website, where the clients will see. after looking at my database, I couldn't find any reference to "artwork_approved." Im guessing it didn't get inputted into the db because I didn't register correct.
    – gdinari
    Commented May 31, 2011 at 16:21
  • @gdinari I'm not familiar with Register Plus Redux so I can't help you with that. As for the code in my answer both the first and second blocks should be in the plugin. The complete plugin should look something like this gist.github.com/1000916. The final block (the condition) can be used wherever you need to check the value of artwork_approved.
    – Richard M
    Commented May 31, 2011 at 17:27
1

Shouldn't the second block of code read:

add_action('personal_options_update', 'my_user_profile_update_action');
add_action('edit_user_profile_update', 'my_user_profile_update_action');
function my_user_profile_update_action($user_id) {
  update_user_meta($user_id, 'artwork_approved', $_POST['artwork_approved']);
}

The value saved by update_user_meta is $_POST['artwork_approved'] not isset($_POST['artwork_approved']).

2
  • Good point, though I would suggest sanitizing the data like so: update_user_meta($user_id, 'artwork_approved', isset($_POST['artwork_approved']) ? 1 : 0 ); Commented Dec 17, 2012 at 5:25
  • 1
    As it's using a checkbox 'artwork_approved' won't appear in the $_POST array if it's unchecked.
    – Richard M
    Commented Dec 18, 2012 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.