如果你要折騰主題或者將WordPress站點(diǎn)開放注冊,你可能需要自定義WordPress用戶個(gè)人資料信息。下面倡萌將簡單說一下如何刪除、添加和調(diào)用自定義用戶信息字段。
來源于:
https://www.wpdaxue.com/add-remove-display-wordpress-user-profile-fields.html
/**
* 自定義用戶個(gè)人資料信息
* https://www.wpdaxue.com/add-remove-display-wordpress-user-profile-fields.html
*/
add_filter( 'user_contactmethods', 'wpdaxue_add_contact_fields' );
function wpdaxue_add_contact_fields( $contactmethods ) {
$contactmethods['qq'] = 'QQ';
$contactmethods['qm_mailme'] = 'QQ郵箱“郵我”';
$contactmethods['qq_weibo'] = '騰訊微博';
$contactmethods['sina_weibo'] = '新浪微博';
$contactmethods['twitter'] = 'Twitter';
$contactmethods['google_plus'] = 'Google+';
$contactmethods['donate'] = '贊助鏈接';
unset( $contactmethods['yim'] );
unset( $contactmethods['aim'] );
unset( $contactmethods['jabber'] );
return $contactmethods;
}
以上代碼通過?user_contactmethods?這個(gè)鉤子添加了QQ等多個(gè)自定義字段,同時(shí)移除了 yim、aim和jabber,用法一目了然,就不多說,效果如下圖所示:

如果要調(diào)用上面的字段,只需要使用 the_author_meta() 或 get_the_author_meta() 這兩個(gè)函數(shù)即可。
the_author_meta() 直接打印輸出字段值
get_the_author_meta() 返回字段值給其他函數(shù)調(diào)用
注:一般而言,WordPress大多數(shù)函數(shù)都有類似這兩種,一個(gè)帶 get_ 前綴,一個(gè)沒有,兩者用法的區(qū)別如上所說。
比如我們要調(diào)用QQ字段,可以使用使用下面的代碼:
<?php
//打印輸出QQ字段的值
the_author_meta( 'qq' );
//或者下面的
echo get_the_author_meta( 'qq' );
?>
我們在實(shí)際使用的時(shí)候,最好先通過 IF 語句判斷用戶是否填寫了 QQ 這個(gè)字段(即判斷QQ字段是否存在值),如果填寫了,就輸出,否者不輸出
<?php if ( get_the_author_meta( 'qq' ) ){
echo '作者QQ:'.get_the_author_meta( 'qq' );
}
關(guān)于調(diào)用更多默認(rèn)的字段,建議大家自己參考?the_author_meta()?和?get_the_author_meta()
注:在沒有指定用戶ID等明確信息時(shí),以上兩個(gè)函數(shù)只能在循環(huán)(Loop)內(nèi)才能正常使用。
WordPress 個(gè)人資料添加額外的字段
來源于:
https://www.wpdaxue.com/extra-user-profile-fields.html
在以上的方法中,我們可以非常方便地自定義“聯(lián)系信息”表單,但是那個(gè)方法有些弊端:
只能新增到“聯(lián)系信息”那里,不能添加自定義的描述文字(提示文本),只能是 input 表單。
今天分享的方法就可以彌補(bǔ)這幾個(gè)弊端,可以將字段添加到所有資料的最下面,支持添加描述文字,可以使用 input、textarea、select 等多種表單(前提是你會用)。
下面是一個(gè)簡單的樣例,添加標(biāo)題和兩個(gè) input 表單。

在主題的 functions.php 里添加下面的代碼:
/**
* WordPress 個(gè)人資料添加額外的字段
* https://www.wpdaxue.com/extra-user-profile-fields.html
*/
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("Facebook URL"); ?></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("請輸入您的 Facebook 地址"); ?></span>
</td>
</tr>
<tr>
<th><label for="twitter"><?php _e("Twitter"); ?></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("請輸入您的 Twitter 用戶名"); ?></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'] );
}
代碼中使用了 show_user_profile 和 edit_user_profile 這兩個(gè)鉤子將表單掛載到個(gè)人資料頁面,然后使用 ‘personal_options_update’ 和 ‘edit_user_profile_update’ 這兩個(gè)鉤子掛載新添加的字段到更新操作,其中使用 update_usermeta() 這個(gè)函數(shù)來更新字段信息。
如果你是開發(fā)者,相信你自己可以添加其他的表單類型,倡萌就不獻(xiàn)丑了。