WordPress 站点默认的用户个人资料项目相对比较少,对于有较多用户或作者的站点而言,不利于大家的互动交流,用户体验也会有所下降,所以我们非常有必要在个人资料中新增加一些额外的字段,比如 QQ 信息、微信信息等。下面 boke112 就来为大家说一下如何添加 WordPress 用户个人资料的额外字段信息。
第一种情况:在个人资料中的『联系信息』直接添加
如果只是想单纯地增加一个用户的 QQ、微信、微博、赞赏地址等比较简单的个人信息,我们只需要将它们直接添加到个人资料中的『联系信息』中添加相应的作者信息字段即可。方法也很简单,只需要将以下代码添加到我们主题的 functions.php 文件中即可。
- // 联系信息中添加作者信息字段
- add_filter(‘user_contactmethods’, ‘boke112_user_contact’);
- function boke112_user_contact($user_contactmethods){
- unset($user_contactmethods[‘aim’]);
- unset($user_contactmethods[‘yim’]);
- unset($user_contactmethods[‘jabber’]);
- $user_contactmethods[‘qq’] = ‘QQ’;
- $user_contactmethods[‘weixin’] = ‘微信号’;
- $user_contactmethods[‘weibo’] = ‘新浪微博’;
- $user_contactmethods[‘toutiao’] = ‘头条号’;
- $user_contactmethods[‘zanshang’] = ‘赞赏作者’;
- return $user_contactmethods;
- }
说明:以上默认添加 5 个作者信息字段,添加代码时请自行修改后添加。
弊端:这种方法相对比较简单,但是只能新增到“联系信息”那里,不能添加自定义的描述文字(提示文本),只能是 input 表单。
第二种情况:在个人资料中增加额外的信息项
如果我们不想直接添加在个人资料的联系信息中,而且想要添加的信息也比较复杂,比如除了需要添加 input 表单外,还需要添加 textarea 类型的表单。此时,我们只需要将以下代码添加到我们主题的 functions.php 文件中即可。
- //个人资料中直接添加额外的作者信息
- 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>额外信息</h3>
- <table class=“form-table”>
- <tr>
- <th><label for=“qq”>QQ在线</label></th>
- <td>
- <input type=“text” name=“qq” id=“qq” value=“<?php echo esc_attr( get_the_author_meta( ‘qq’, $user->ID ) ); ?>” class=“regular-text” /><br />
- <span class=“description”>投稿作者的QQ在线</span>
- </td>
- </tr>
- <tr>
- <th><label for=“weixin”>微信号</label></th>
- <td>
- <input type=“text” name=“weixin” id=“weixin” value=“<?php echo esc_attr( get_the_author_meta( ‘weixin’, $user->ID ) ); ?>” class=“regular-text” /><br />
- <span class=“description”>投稿作者的微信二维码地址</span>
- </td>
- </tr>
- <tr>
- <th><label for=“weibo”>新浪微博</label></th>
- <td>
- <input type=“text” name=“weibo” id=“weibo” value=“<?php echo esc_attr( get_the_author_meta( ‘weibo’, $user->ID ) ); ?>” class=“regular-text” /><br />
- <span class=“description”>投稿作者的新浪微博地址</span>
- </td>
- </tr>
- <tr>
- <th><label for=“toutiao”>头条号</label></th>
- <td>
- <input type=“text” name=“toutiao” id=“toutiao” value=“<?php echo esc_attr( get_the_author_meta( ‘toutiao’, $user->ID ) ); ?>” class=“regular-text” /><br />
- <span class=“description”>投稿作者的头条号地址</span>
- </td>
- </tr>
- <tr>
- <th><label for=“zanshang”>赞赏作者</label></th>
- <td>
- <input type=“text” name=“zanshang” id=“zanshang” value=“<?php echo esc_attr( get_the_author_meta( ‘zanshang’, $user->ID ) ); ?>” class=“regular-text” /><br />
- <span class=“description”>赞赏作者的二维码地址</span>
- </td>
- </tr>
- <tr>
- <th><label for=“guanggaoyu”>广告语</label></th>
- <td><textarea name=“guanggaoyu” id=“guanggaoyu” rows=“5” cols=“30”><?php echo esc_attr( get_the_author_meta( ‘guanggaoyu’, $user->ID ) ); ?></textarea>
- <p class=“description”>投稿作者的广告语</p></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_usermeta( $user_id, ‘qq’, $_POST[‘qq’] );
- update_usermeta( $user_id, ‘weixin’, $_POST[‘weixin’] );
- update_usermeta( $user_id, ‘weibo’, $_POST[‘weibo’] );
- update_usermeta( $user_id, ‘toutiao’, $_POST[‘toutiao’] );
- update_usermeta( $user_id, ‘zanshang’, $_POST[‘zanshang’] );
- update_usermeta( $user_id, ‘guanggaoyu’, $_POST[‘guanggaoyu’] );
- }
说明:以上只是本站用到的一个比较简单的样例,含有几条input 表单和一条textarea类型的表单。大家添加时请自行修改成其他类型的表单即可。
代码修改自WordPress大学
增加的个人信息字段如何引用?
在个人资料中添加好额外的用户信息后,我们还需要在站点某个位置引用这些信息,此时,我们只需要使用 the_author_meta() 或 get_the_author_meta() 这两个函数即可。比如我们想输出或获取 QQ 字段的值,可以这样获得:
- <?php
- //直接输出QQ字段的值
- the_author_meta( ‘qq’ );
- //这个也是输出QQ字段的值
- echo get_the_author_meta( ‘qq’ );
- ?>
说明:以上这两个函数用来获取网站用户的信息,如果在循环中则不需要指定用户的 ID,直接调用当前文章的作者的信息,否则必须得指定需要获取的用户的 ID 才行。同时,在获取输入这些字段值时,建议加上一个条件判断会比较好,比如输入作者 QQ,首先判断作者 QQ 这个字段是否有值,有的情况下才输出。
- <?php if ( get_the_author_meta( ‘qq’ ) ){
- echo ‘作者QQ:’.get_the_author_meta( ‘qq’ );
- }?>
再比如想获取并输出 ID 为 5 的作者 QQ 号,可以参照以下代码:
- <?php if ( get_the_author_meta( ‘qq’ ,5) ){
- echo ‘作者QQ:’.get_the_author_meta( ‘qq’,5 );
- }?>
get_the_author_meta() 函数具体用法
get_the_author_meta( $field, $user_id );
参数:
$field:(字符串)(可选)要返回的数据项,默认值:None,可选值:
- user_login
- user_pass
- user_nicename
- user_email
- user_url
- user_registered
- user_activation_key
- user_status
- roles
- display_name
- nickname
- first_name
- last_name
- description
- jabber
- aim
- yim
- user_level
- user_firstname
- user_lastname
- rich_editing
- comment_shortcuts
- admin_color
- plugins_per_page
- plugins_last_view
- ID
$user_id:(整数)(可选)需要返回的数据的用户 ID。如果在循环里可以不指定,会自动指定为当前文章的作者。默认值:None