阿里云服务器

今天给大家分享一种非插件,拓展wordpress个人资料 ,新增字段调用到模板的方法!

这样在做wordpress作者页模板的时候就可以新增需要的字段进行调用!

下面是效果演示:

只需在主题函数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><?php _e("新增用户信息", "blank"); ?></h3>  
   
<table class="form-table">  
    <tr>  
        <th><label for="facebook"><?php _e("客座教授、研究员"); ?></label></th>  
        <td>  
            <input type="text" name="facebook" id="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" class="regular-text" /><br />  
            <span class="description"><?php _e("请输入客座教授、研究员"); ?></span>  
        </td>  
    </tr>  
    <tr>  
        <th><label for="twitter"><?php _e("擅长领域"); ?></label></th>  
        <td>  
            <input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />  
            <span class="description"><?php _e("擅长领域"); ?></span>  
        </td>  
    </tr>  
  
  
    <tr>  
        <th><label for="kecheng"><?php _e("主讲课程"); ?></label></th>  
        <td>  
  
<textarea name="kecheng" rows="3" cols="50" id="blacklist_keys" class="large-text code" innerHTML=""><?php echo esc_attr( get_the_author_meta( 'kecheng', $user->ID ) ); ?></textarea><br />  
  
  
            <span class="description"><?php _e("主讲课程"); ?></span>  
        </td>  
    </tr>  
  
  
    <tr>  
        <th><label for="weixin"><?php _e("二维码"); ?></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"><?php _e("二维码"); ?></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_usermeta( $user_id, 'facebook', $_POST['facebook'] );  
    update_usermeta( $user_id, 'twitter', $_POST['twitter'] );  
    update_usermeta( $user_id, 'kecheng', $_POST['kecheng'] );  
  
    update_usermeta( $user_id, 'weixin', $_POST['weixin'] );  
  
}

代码中使用了 show_user_profile 和 edit_user_profile 这两个钩子将表单挂载到个人资料页面,然后使用 'personal_options_update' 和 'edit_user_profile_update' 这两个钩子挂载新添加的字段到更新操作,其中使用 update_usermeta() 这个函数来更新字段信息。


相关阅读:
  • wordpress实现同分类上下篇文章功能
  • 批量修改wordpress文章内容的方法
  • wordpress作者信息调用代码大全