腾讯云服务器优惠活动华纳云香港服务器

如何添加WordPress用户个人资料的额外字段信息?

WordPress主题推荐

WordPress 站点默认的用户个人资料项目相对比较少,对于有较多用户或作者的站点而言,不利于大家的互动交流,用户体验也会有所下降,所以我们非常有必要在个人资料中新增加一些额外的字段,比如 QQ 信息、微信信息等。下面 boke112 就来为大家说一下如何添加 WordPress 用户个人资料的额外字段信息。

如何添加WordPress用户个人资料的额外字段信息?-第1张-boke112百科(boke112.com)

第一种情况:在个人资料中的『联系信息』直接添加

如果只是想单纯地增加一个用户的 QQ、微信、微博、赞赏地址等比较简单的个人信息,我们只需要将它们直接添加到个人资料中的『联系信息』中添加相应的作者信息字段即可。方法也很简单,只需要将以下代码添加到我们主题的 functions.php 文件中即可。

  1. // 联系信息中添加作者信息字段
  2. add_filter(‘user_contactmethods’, ‘boke112_user_contact’);
  3. function boke112_user_contact($user_contactmethods){
  4.     unset($user_contactmethods[‘aim’]);
  5.     unset($user_contactmethods[‘yim’]);
  6.     unset($user_contactmethods[‘jabber’]);
  7.     $user_contactmethods[‘qq’] = ‘QQ’;
  8.     $user_contactmethods[‘weixin’] = ‘微信号’;
  9.     $user_contactmethods[‘weibo’] = ‘新浪微博’;
  10.     $user_contactmethods[‘toutiao’] = ‘头条号’;
  11.     $user_contactmethods[‘zanshang’] = ‘赞赏作者’;
  12.     return $user_contactmethods;
  13. }

说明:以上默认添加 5 个作者信息字段,添加代码时请自行修改后添加。

弊端:这种方法相对比较简单,但是只能新增到“联系信息”那里,不能添加自定义的描述文字(提示文本),只能是 input 表单。

如何添加WordPress用户个人资料的额外字段信息?-第2张-boke112百科(boke112.com)

第二种情况:在个人资料中增加额外的信息项

如果我们不想直接添加在个人资料的联系信息中,而且想要添加的信息也比较复杂,比如除了需要添加 input 表单外,还需要添加 textarea 类型的表单。此时,我们只需要将以下代码添加到我们主题的 functions.php 文件中即可。

  1. //个人资料中直接添加额外的作者信息
  2. add_action( ‘show_user_profile’, ‘extra_user_profile_fields’ );
  3. add_action( ‘edit_user_profile’, ‘extra_user_profile_fields’ );
  4. function extra_user_profile_fields( $user ) { ?>
  5. <h3>额外信息</h3>
  6. <table class=“form-table”>
  7.     <tr>
  8.         <th><label for=“qq”>QQ在线</label></th>
  9.         <td>
  10.             <input type=“text” name=“qq” id=“qq” value=“<?php echo esc_attr( get_the_author_meta( ‘qq’, $user->ID ) ); ?>” class=“regular-text” /><br />
  11.             <span class=“description”>投稿作者的QQ在线</span>
  12.         </td>
  13.     </tr>
  14.     <tr>
  15.         <th><label for=“weixin”>微信号</label></th>
  16.         <td>
  17.             <input type=“text” name=“weixin” id=“weixin” value=“<?php echo esc_attr( get_the_author_meta( ‘weixin’, $user->ID ) ); ?>” class=“regular-text” /><br />
  18.             <span class=“description”>投稿作者的微信二维码地址</span>
  19.         </td>
  20.     </tr>
  21.     <tr>
  22.         <th><label for=“weibo”>新浪微博</label></th>
  23.         <td>
  24.             <input type=“text” name=“weibo” id=“weibo” value=“<?php echo esc_attr( get_the_author_meta( ‘weibo’, $user->ID ) ); ?>” class=“regular-text” /><br />
  25.             <span class=“description”>投稿作者的新浪微博地址</span>
  26.         </td>
  27.     </tr>
  28.     <tr>
  29.         <th><label for=“toutiao”>头条号</label></th>
  30.         <td>
  31.             <input type=“text” name=“toutiao” id=“toutiao” value=“<?php echo esc_attr( get_the_author_meta( ‘toutiao’, $user->ID ) ); ?>” class=“regular-text” /><br />
  32.             <span class=“description”>投稿作者的头条号地址</span>
  33.         </td>
  34.     </tr>
  35.     <tr>
  36.         <th><label for=“zanshang”>赞赏作者</label></th>
  37.         <td>
  38.             <input type=“text” name=“zanshang” id=“zanshang” value=“<?php echo esc_attr( get_the_author_meta( ‘zanshang’, $user->ID ) ); ?>” class=“regular-text” /><br />
  39.             <span class=“description”>赞赏作者的二维码地址</span>
  40.         </td>
  41.     </tr>
  42.     <tr>
  43.         <th><label for=“guanggaoyu”>广告语</label></th>
  44.         <td><textarea name=“guanggaoyu” id=“guanggaoyu” rows=“5” cols=“30”><?php echo esc_attr( get_the_author_meta( ‘guanggaoyu’, $user->ID ) ); ?></textarea>
  45.         <p class=“description”>投稿作者的广告语</p></td>
  46.     </tr>
  47. </table>
  48. <?php }
  49. add_action( ‘personal_options_update’, ‘save_extra_user_profile_fields’ );
  50. add_action( ‘edit_user_profile_update’, ‘save_extra_user_profile_fields’ );
  51. function save_extra_user_profile_fields( $user_id ) {
  52.     if ( !current_user_can( ‘edit_user’, $user_id ) ) { return false; }
  53.     update_usermeta( $user_id, ‘qq’, $_POST[‘qq’] );
  54.     update_usermeta( $user_id, ‘weixin’, $_POST[‘weixin’] );
  55.     update_usermeta( $user_id, ‘weibo’, $_POST[‘weibo’] );
  56.     update_usermeta( $user_id, ‘toutiao’, $_POST[‘toutiao’] );
  57.     update_usermeta( $user_id, ‘zanshang’, $_POST[‘zanshang’] );
  58.     update_usermeta( $user_id, ‘guanggaoyu’, $_POST[‘guanggaoyu’] );
  59. }

说明:以上只是本站用到的一个比较简单的样例,含有几条input 表单和一条textarea类型的表单。大家添加时请自行修改成其他类型的表单即可。

代码修改自WordPress大学

增加的个人信息字段如何引用?

在个人资料中添加好额外的用户信息后,我们还需要在站点某个位置引用这些信息,此时,我们只需要使用 the_author_meta() 或 get_the_author_meta() 这两个函数即可。比如我们想输出或获取 QQ 字段的值,可以这样获得:

  1. <?php
  2. //直接输出QQ字段的值
  3. the_author_meta( ‘qq’ );
  4. //这个也是输出QQ字段的值
  5. echo get_the_author_meta( ‘qq’ );
  6. ?>

说明:以上这两个函数用来获取网站用户的信息,如果在循环中则不需要指定用户的 ID,直接调用当前文章的作者的信息,否则必须得指定需要获取的用户的 ID 才行。同时,在获取输入这些字段值时,建议加上一个条件判断会比较好,比如输入作者 QQ,首先判断作者 QQ 这个字段是否有值,有的情况下才输出。

  1. <?php if ( get_the_author_meta( ‘qq’ ) ){
  2.     echo ‘作者QQ:’.get_the_author_meta( ‘qq’ );
  3. }?>

再比如想获取并输出 ID 为 5 的作者 QQ 号,可以参照以下代码:

  1. <?php if ( get_the_author_meta( ‘qq’ ,5) ){
  2.     echo ‘作者QQ:’.get_the_author_meta( ‘qq’,5 );
  3. }?>

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

本文地址:https://boke112.com/post/3829.html

赞 (0) 打赏
版权声明:本文为原创文章,版权归 boke112百科 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
香港云主机55元/年
wu