最新公告
  • 欢迎您光临牛品源码,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入我们
  • WordPress教程之添加用户自定义字段(user_meta)到用户资料

    在我们进行WordPress前端用户中心功能开发的时候,经常会需要一些自定义字段来实现我们的需求,很多的插件可以实现我们的需求,今天牛品源码分享的是免插件实现添加用户自定义字段(user_meta)到用户资料的教程。

    今天我们来看一下怎么通过代码免插件实现添加用户自定义字段。

    功能截图

    WordPress教程之添加用户自定义字段(user_meta)到用户资料

    添加用户自定义字段

    首先我们在用户资料编辑页面添加一行“微信号码” 的自定义字段。直接把下面的代码复制到主题的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 class=\"form-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 class=\"description\">请输入微博用户名。</span>
            </td>
        </tr>
    </table>
    <?php }?>
    

    调用自定义字段

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

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

    今天的wordpress教程对您有没有一些帮助呢?如果有的话请分享给其他需要的朋友。

    文章来自牛品源码www.zjw13.xyz转载请保留本链接,谢谢!

    发表评论

    售后服务:

    • 售后服务范围 1、商业模板使用范围内问题免费咨询
      2、源码安装、模板安装(一般 ¥50-500)服务答疑仅限SVIP用户
      3、单价超过200元的模板免费一次安装,需提供服务器信息。
      付费增值服务 1、提供dedecms模板、WordPress主题、discuz模板优化等服务请详询在线客服
      2、承接 WordPress、DedeCMS、Discuz 等系统建站、仿站、开发、定制等服务
      3、服务器环境配置(一般 ¥50-500)
      4、网站中毒处理(需额外付费,1000元/次/质保三个月)
      售后服务时间 周一至周日(法定节假日除外) 9:00-23:00
      免责声明 本站所提供的模板(主题/插件)等资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: niupinyuanma@foxmail.com),我们会及时删除,给您带来的不便,我们深表歉意!