Wordpress: creating a new usermeta field for users

How to create a new usermeta field with dropdown selection values?

I want to create a conditional statement for all users with a specific value for the new custom field that I want.

For instance,

New field will be: Approved Drop-down values: Yes and No

The conditional statement recognizes all users with the Yes field value set. Then it will output the code.

Im working with the wp_get_current_user () function, which does exactly what I need, but I just need a new usermeta user field. In this example, the new usermeta field will 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 advice on creating a new utility with drop-down options.

* UPDATE:

I used Register Plus Redux to create a new usermeta field called Approved Jobs. I chose the option with the option "No" and "Yes." No is the default setting.

This created a "usermeta" field labeled "Artwork Approved". I manage user accounts and select "Yes" or "No". Now with this new โ€œusermetaโ€ field, I am using a function that should check if the current user has an approved work with a value of โ€œYesโ€. Then it is supposed to show a specific code.

Here is the if statement using usermeta with the new 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 happens is not recognition of the first part of the if statement. If I log into any account with an approved cover, the if statement only shows โ€œelse,โ€ even if I have the โ€œYesโ€ option for the approved artwork.

I do not know why it does not recognize the Yes parameter as it is in the instruction.

thanks

+6
source share
2 answers

You can create a simple plugin to connect to user profile actions and add a new field.

To add a field to the form, you can connect to the show_user_profile and edit_user_profile and display the HTML form field. The example below uses a check box rather than a drop-down list.

 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 connect to the actions of personal_options_update and edit_user_profile_update , get the value of your field and save it as a meta-user.

 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 will be as shown below.

 if (get_user_meta($current_user->ID, 'artwork_approved', true)) { 
+16
source

You should not read the second block of code:

 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 stored by update_user_meta is $_POST['artwork_approved'] not isset($_POST['artwork_approved']) .

+1
source

Source: https://habr.com/ru/post/889131/


All Articles