admin管理员组文章数量:1333442
I'm not very good with computers/codes etc. I use a plugin that makes a registration form thingy and in that form I added country, age group, gender and so on. I click the option that will add the registerer into the wordpress user thingy. But when I try it, only the username and email show on the Users section on the backend.. Is there a way for the other fields to show on the users section?
I need them to show for statistical uses.
I'm not very good with computers/codes etc. I use a plugin that makes a registration form thingy and in that form I added country, age group, gender and so on. I click the option that will add the registerer into the wordpress user thingy. But when I try it, only the username and email show on the Users section on the backend.. Is there a way for the other fields to show on the users section?
I need them to show for statistical uses.
Share Improve this question asked Jan 16, 2016 at 8:40 Chloe AusChloe Aus 4611 gold badge5 silver badges3 bronze badges 1- +1 for the intro - may end up borrowing it! :-D – Boycott A.I. Commented Nov 12, 2024 at 19:02
4 Answers
Reset to default 106You need to use the show_user_profile
, edit_user_profile
, personal_options_update
, and edit_user_profile_update
hooks.
You can use the following code for adding additional fields in User section
Code for adding extra fields in Edit User Section:
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="address"><?php _e("Address"); ?></label></th>
<td>
<input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your address."); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e("City"); ?></label></th>
<td>
<input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your city."); ?></span>
</td>
</tr>
<tr>
<th><label for="postalcode"><?php _e("Postal Code"); ?></label></th>
<td>
<input type="text" name="postalcode" id="postalcode" value="<?php echo esc_attr( get_the_author_meta( 'postalcode', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your postal code."); ?></span>
</td>
</tr>
</table>
<?php }
Code for saving extra fields details in database:
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $user_id ) ) {
return;
}
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'address', $_POST['address'] );
update_user_meta( $user_id, 'city', $_POST['city'] );
update_user_meta( $user_id, 'postalcode', $_POST['postalcode'] );
}
There are also several blog posts available on the subject that might be helpful:
- Adding and using custom user profile fields
- Adding Extra Fields to the WordPress User Profile
The Advanced Custom Fields Pro plugin will allow you to add fields to user profiles without any coding.
You'd better use get_user_meta
(instead of get_the_author_meta
):
function extra_user_profile_fields( $user ) {
$meta = get_user_meta($user->ID, 'meta_key_name', false);
}
You can also easily add additional fields to the existing "Contact Info" section of the user profile edit screen, and it doesn't require set up of other hooks to update the meta fields.
function custom_user_profile_contact_fields( $methods ) {
$methods['phone'] = 'Phone number';
$methods['location'] = 'Location';
return $methods;
}
add_action( 'user_contactmethods', 'custom_user_profile_contact_fields' );
本文标签: How do I add a field on the Users profile For examplecountryage etc
版权声明:本文标题:How do I add a field on the Users profile? For example, country, age etc 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737468200a1991299.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论