如何在“用户”个人资料上添加字段?例如国家/地区,年龄等
-
-
请尝试我们的搜索.您会发现许多示例.Please try our search. You will find dozens of examples.
- 5
- 2016-01-16
- fuxia
-
3 个回答
- 投票数
-
- 2016-01-16
您需要使用
show_user_profile
,edit_user_profile
,personal_options_update
和edit_user_profile_update
钩子.您可以使用以下代码在"用户"部分添加其他字段
在"编辑用户"部分中添加其他字段的代码:
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 }
用于在数据库中保存额外字段详细信息的代码:
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 ( !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'] ); }
关于该主题的一些博客文章也可能会有所帮助:
You need to use the
show_user_profile
,edit_user_profile
,personal_options_update
, andedit_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 ( !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:
-
太棒了,这很棒.Bravo this works great.
- 0
- 2018-03-15
- AVEbrahimi
-
这不是在数据库中存储我额外字段中的数据.有什么建议吗?谢谢.This isn't storing data from my extra fields in the DB. Suggestions please? Thx.
- 1
- 2019-07-03
- b_dubb
-
@b_dubb,能否请您分享您的代码?因此,我将检查并告知您.@b_dubb, Can you please share your code? So i'll check and let you know.
- 0
- 2019-08-02
- Arpita Hunka
-
我已经解决了我的问题,但感谢您的支持.I have resolved my issue but thanks for reaching out.
- 0
- 2019-08-05
- b_dubb
-
您应该为此添加随机数验证,以避免引入安全漏洞.https://developer.wordpress.org/themes/theme-security/using-nonces/You should add nonce verification to this to avoid introducing security vulnerabilities. https://developer.wordpress.org/themes/theme-security/using-nonces/
- 1
- 2020-01-30
- squarecandy
-
- 2017-09-20
高级自定义字段专业插件将使您无需任何编码即可将字段添加到用户配置文件
The Advanced Custom Fields Pro plugin will allow you to add fields to user profiles without any coding.
-
仅专业版Only the pro version
- 3
- 2019-03-04
- I am the Most Stupid Person
-
有免费的方法可以用PHP做到这一点.There are free ways of doing this with PHP.
- 2
- 2019-10-15
- Drmzindec
-
是的-如果您愿意,绝对可以在没有ACF的情况下使用PHP编写此代码.我的经验是,它需要100多行代码,并且您需要担心随机数验证,编写表单HTML等问题.与ACF中的5-10分钟的设置相比,可能需要花费几个小时的编码时间.可能取决于您是否已经在项目中使用ACF Pro.Yep - definitely possible to code this in PHP without ACF if you prefer. My experience is that it takes 100+ lines of code and you need to worry about nonce verification, writing the HTML of the form, etc. Could take a few hours of coding vs. 5-10 min of setup in ACF. Probably depends on if you're using ACF Pro already on a project.
- 0
- 2019-10-15
- squarecandy
-
Wordpress应该做到这一点,而不要求您在php中对html表单进行硬编码.我第二个ACF,它应该是核心部分.您还可以使用代码定义字段并对其进行版本控制.Wordpress should do this without asking you to hardcode html forms in php. I second ACF, it should be part of the core. You can also define fields with code and version it.
- 2
- 2020-01-30
- marek.m
-
- 2018-12-04
您最好使用
get_user_meta
(而不是get_the_author_meta
):function extra_user_profile_fields( $user ) { $meta = get_user_meta($user->ID, 'meta_key_name', false); }
You'd better use
get_user_meta
(instead ofget_the_author_meta
):function extra_user_profile_fields( $user ) { $meta = get_user_meta($user->ID, 'meta_key_name', false); }
-
两者都没有问题!both works with no problems!
- 0
- 2020-08-18
- Fernando Baltazar
我对计算机/代码等不是很好. 我使用的插件使注册表单变得很重要,并在表单中添加了国家/地区,年龄段,性别等信息.我单击将注册器添加到wordpress用户thingy中的选项.但是当我尝试时,后端的"用户"部分仅显示用户名和电子邮件. 有其他字段可以在用户部分显示吗?
我需要它们显示出来用于统计用途.