首页 WordPress教程 WordPress个人资料添加自定义字段input表单
正文 评论

WordPress个人资料添加自定义字段input表单

前几天有人问主题君,怎么在我的个人资料这个页面添加其他的字段呢?例如:QQ号码、微信号码之类的input表单输入框。

WordPress默认的设置里是没有QQ号码、微信号码的,如果要添加我们只能使用代码添加自定义字段,有很多插件可以实现这个功能,但是能不用插件就不用插件嘛,今天主题君分享一篇免插件纯代码实现个人资料添加自定义字段的WordPress教程。

添加用户自定义字段

首先我们在我的个人资料编辑页面添加一行“微信号码”的自定义字段。

直接把下面的代码复制到主题的functions.php文件中,即可在用户资料编辑页面看到一个“QQ号码”的表单项。

add_action( \'show_user_profile\', \'wizhi_extra_user_profile_fields\' );add_action( \'edit_user_profile\', \'wizhi_extra_user_profile_fields\' );add_action( \'personal_options_update\', \'wizhi_save_extra_user_profile_fields\' );add_action( \'edit_user_profile_update\', \'wizhi_save_extra_user_profile_fields\' );function wizhi_save_extra_user_profile_fields( $user_id ){if ( !current_user_can( \'edit_user\', $user_id ) ) { return false; }    update_user_meta( $user_id, \'wx_username\', $_POST[\'wx_username\'] );}function wizhi_extra_user_profile_fields( $user ){ ?><h3>附加用户字段</h3><table >    <tr>        <th><label for=\"wx_username\">微博用户名</label></th>        <td>            <input type=\"text\" id=\"wx_username\" name=\"wx_username\" size=\"20\" value=\"<?php echo esc_attr( get_the_author_meta( \'wx_user_name\', $user->ID )); ?>\">            <span >请输入微博用户名。</span>        </td>    </tr></table><?php }?>

调用自定义字段

添加好用户自定义字段后,如何获取使用这个字段呢?获取的方法也很简单,示例代码如下:

<?php$current_user = wp_get_current_user();get_user_meta( $current_user->ID, \'wx_username\', true); ?>

把上面的调用自定义字段添加到想要显示用户字段内容的页面即可。

扫码关注wp主题君

精选优质免费WordPress主题模板,分享最新WordPress实用建站教程!

记住我们的网址:ztJun.com

-=||=-收藏赞 (0)
更多主题
v1.0.0
小程序收录免费WordPress主题:XCX主题
¥ 0 关注:22,655
详情
单栏极简文艺WordPress博客主题:Diaspora主题
¥ 0 关注:22,141
详情
简约黑白WordPress个人博客主题:Personal主题
¥ 0 关注:21,870
详情
功能丰富切轻量的WordPress虚拟资源主题:Rizhuti主题
¥ 599 关注:19,939
详情
v1.0.0
一款简约单栏的免费WordPress博客主题:itheme主题
¥ 0 关注:19,855
详情
WordPress版WebStack导航主题:WebStack主题
¥ 0 关注:16,967
详情
回复
暂无评论

不要再留垃圾评论了,主题君整理资源不容易,留几句鼓励的话吧。