刪除/添加/調用WordPress用戶個人資料的聯系信息

    在自己的設置頁添加幾個設置項目=

    如果你要折騰主題或者將WordPress站點開放注冊,你可能需要自定義WordPress用戶個人資料信息。下面倡萌將簡單說一下如何刪除、添加和調用自定義用戶信息字段。

    來源于:

    https://www.wpdaxue.com/add-remove-display-wordpress-user-profile-fields.html

    /**
     * 自定義用戶個人資料信息
     * 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?這個鉤子添加了QQ等多個自定義字段,同時移除了 yim、aim和jabber,用法一目了然,就不多說,效果如下圖所示:

    刪除/添加/調用WordPress用戶個人資料的聯系信息

    如果要調用上面的字段,只需要使用 the_author_meta() 或 get_the_author_meta() 這兩個函數即可。

    the_author_meta() 直接打印輸出字段值

    get_the_author_meta() 返回字段值給其他函數調用

    注:一般而言,WordPress大多數函數都有類似這兩種,一個帶 get_ 前綴,一個沒有,兩者用法的區別如上所說。

    比如我們要調用QQ字段,可以使用使用下面的代碼:

    <?php
    //打印輸出QQ字段的值
    the_author_meta( 'qq' );
    //或者下面的
    echo get_the_author_meta( 'qq' );
    ?>
    

    我們在實際使用的時候,最好先通過 IF 語句判斷用戶是否填寫了 QQ 這個字段(即判斷QQ字段是否存在值),如果填寫了,就輸出,否者不輸出

    <?php if ( get_the_author_meta( 'qq' ) ){
    	echo '作者QQ:'.get_the_author_meta( 'qq' );
    }
    

    關于調用更多默認的字段,建議大家自己參考?the_author_meta()?和?get_the_author_meta()

    注:在沒有指定用戶ID等明確信息時,以上兩個函數只能在循環(Loop)內才能正常使用。

    WordPress 個人資料添加額外的字段

    來源于:

    https://www.wpdaxue.com/extra-user-profile-fields.html

    在以上的方法中,我們可以非常方便地自定義“聯系信息”表單,但是那個方法有些弊端:

    只能新增到“聯系信息”那里,不能添加自定義的描述文字(提示文本),只能是 input 表單。

    今天分享的方法就可以彌補這幾個弊端,可以將字段添加到所有資料的最下面,支持添加描述文字,可以使用 input、textarea、select 等多種表單(前提是你會用)。

    下面是一個簡單的樣例,添加標題和兩個 input 表單。

    刪除/添加/調用WordPress用戶個人資料的聯系信息

    在主題的 functions.php 里添加下面的代碼:

    /**
     * WordPress 個人資料添加額外的字段
     * 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 這兩個鉤子將表單掛載到個人資料頁面,然后使用 ‘personal_options_update’ 和 ‘edit_user_profile_update’ 這兩個鉤子掛載新添加的字段到更新操作,其中使用 update_usermeta() 這個函數來更新字段信息。

    如果你是開發者,相信你自己可以添加其他的表單類型,倡萌就不獻丑了。

    教程

    WordPress 禁止用戶注冊某些用戶名

    2019-5-1 0:12:30

    教程

    RevolverMaps - 給網站添加一個小地球

    2019-5-8 20:21:52

    ??
    Npcink上的部份代碼及教程來源于互聯網,僅供網友學習交流,若您喜歡本文可附上原文鏈接隨意轉載。
    無意侵害您的權益,請發送郵件至 1355471563#qq.com 或點擊右側 私信:Muze 反饋,我們將盡快處理。
    0 條回復 A文章作者 M管理員
      暫無討論,說說你的看法吧
    ?
    個人中心
    購物車
    優惠劵
    今日簽到
    有新私信 私信列表
    搜索
    主站蜘蛛池模板: 中文字幕一区二区三区免费视频 | 亚洲一区综合在线播放| 在线观看国产一区亚洲bd| 一区高清大胆人体| 亚洲免费一区二区| 冲田杏梨AV一区二区三区| 日韩精品无码中文字幕一区二区| 国产精品一区二区三区免费| 国产一区二区在线|播放| 91成人爽a毛片一区二区| 亚洲熟妇av一区二区三区| 国产成人一区二区三区高清| 精品一区二区三区| 国产精品久久久久久麻豆一区| 国产精品免费一区二区三区四区| 国产一区二区三区精品视频| 亚洲AV永久无码精品一区二区国产| 国模视频一区二区| 国产精品无码一区二区三区在| 精品无码一区二区三区在线| 久久综合亚洲色一区二区三区 | 亚洲熟妇av一区二区三区下载| 中文字幕精品一区二区三区视频| 亚洲片国产一区一级在线观看| 亚洲国产一区二区视频网站| 国产精品 视频一区 二区三区| 亚洲一区视频在线播放| 一区二区国产在线观看| 国产99精品一区二区三区免费| 美女视频黄a视频全免费网站一区| 久久精品国内一区二区三区| 精品国产一区二区三区久久影院| 欧洲精品码一区二区三区| 在线观看日韩一区| 精品一区二区三区色花堂| 国精品无码A区一区二区| 无码人妻精品一区二区在线视频 | 国产精品一区二区av| 久久青草精品一区二区三区| 亚洲色偷偷偷网站色偷一区| 亚洲国产欧美一区二区三区|