<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>custom-field &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/custom-field/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 12 Dec 2025 14:50:17 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://piglife.tw/wp-content/uploads/2017/10/cropped-logo-1-32x32.png</url>
	<title>custom-field &#8211; 小豬日常</title>
	<link>https://piglife.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>WordPress 使用者頁面新增老師欄位與篩選功能實作筆記</title>
		<link>https://piglife.tw/technical-notes/wordpress-user-teacher-field/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-user-teacher-field/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Tue, 14 Jan 2025 04:58:06 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[admin-filter]]></category>
		<category><![CDATA[custom-field]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[user-list]]></category>
		<category><![CDATA[user-meta]]></category>
		<category><![CDATA[user-profile]]></category>
		<category><![CDATA[WordPress]]></category>
		<guid isPermaLink="false">https://piglife.tw/?p=471</guid>

					<description><![CDATA[介紹如何在 WordPress 使用者編輯頁面新增「是否是老師」勾選欄位，並在使用者列表中顯示及篩選...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在 WordPress 中，管理使用者時常需要新增自訂欄位以標示特定身份。本文針對如何在使用者編輯頁面新增「是否是老師」欄位，並在使用者列表中顯示及篩選此欄位進行說明，適合具備基礎 PHP 與 WordPress 開發經驗的工程師或自學者。</p>
<h2 class="wp-block-heading">一、在使用者編輯頁面新增自訂欄位</h2>
<p>首先，我們要在使用者編輯頁面新增一個勾選框，讓管理員能標記該使用者是否為老師。透過 <code>show_user_profile</code> 與 <code>edit_user_profile</code> 兩個 action，將欄位輸出到前端。</p>
<p>關鍵程式碼片段：</p>
<pre><code class="lang-php language-php php">function ks_add_teacher_user_profile($user) {
    ?&gt;

&lt;h3&gt;老師身分設定&lt;/h3&gt;
    &lt;table class=&quot;form-table&quot;&gt;

&lt;tr&gt;

&lt;th&gt;&lt;label for=&quot;teacher_user&quot;&gt;是否為老師&lt;/label&gt;&lt;/th&gt;

&lt;td&gt;
                &lt;input type=&quot;checkbox&quot; name=&quot;teacher_user&quot; id=&quot;teacher_user&quot; value=&quot;1&quot; &lt;?php checked(get_user_meta($user-&gt;ID, &#039;teacher_user&#039;, true), 1); ?&gt; /&gt;
                &lt;span class=&quot;description&quot;&gt;勾選此選項以標記此使用者為老師。&lt;/span&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
    &lt;?php
}
add_action(&#039;show_user_profile&#039;, &#039;ks_add_teacher_user_profile&#039;);
add_action(&#039;edit_user_profile&#039;, &#039;ks_add_teacher_user_profile&#039;);</code></pre>
<h2 class="wp-block-heading">二、保存欄位數據</h2>
<p>為了將勾選框的值存入資料庫，我們使用 <code>personal_options_update</code> 與 <code>edit_user_profile_update</code> 兩個 action，並利用 <code>update_user_meta</code> 保存。</p>
<p>關鍵程式碼片段：</p>
<pre><code class="lang-php language-php php">function save_teacher_user_profile($user_id) {
    if (!current_user_can(&#039;edit_user&#039;, $user_id)) {
        return false;
    }

    $checkbox_value = isset($_POST[&#039;teacher_user&#039;]) ? 1 : 0;
    update_user_meta($user_id, &#039;teacher_user&#039;, $checkbox_value);
}
add_action(&#039;personal_options_update&#039;, &#039;save_teacher_user_profile&#039;);
add_action(&#039;edit_user_profile_update&#039;, &#039;save_teacher_user_profile&#039;);</code></pre>
<h2 class="wp-block-heading">三、在使用者列表新增自訂欄位</h2>
<p>為了在使用者列表頁面顯示「是否是老師」欄位，我們需要新增欄位標題與欄位內容。</p>
<ol>
<li>
<p>新增欄位標題：</p>
<pre><code class="lang-php language-php php">function ks_add_teacher_user_column($columns) {
 $columns[&#039;teacher_user&#039;] = &#039;老師&#039;;
 return $columns;
}
add_filter(&#039;manage_users_columns&#039;, &#039;ks_add_teacher_user_column&#039;);</code></pre>
</li>
<li>
<p>顯示欄位內容：</p>
<pre><code class="lang-php language-php php">function ks_show_teacher_user_column($value, $column_name, $user_id) {
 if (&#039;teacher_user&#039; === $column_name) {
     $is_teacher_user = get_user_meta($user_id, &#039;teacher_user&#039;, true);
     return $is_teacher_user ? &#039;已啟用&#039; : &#039;-&#039;;
 }
 return $value;
}
add_filter(&#039;manage_users_custom_column&#039;, &#039;ks_show_teacher_user_column&#039;, 10, 3);</code></pre>
</li>
</ol>
<h2 class="wp-block-heading">四、在使用者列表新增篩選功能</h2>
<p>為了方便管理員快速篩選出老師帳號，我們新增篩選按鈕與過濾邏輯。</p>
<ol>
<li>
<p>新增篩選按鈕：</p>
<pre><code class="lang-php language-php php">function ks_add_teacher_user_filter_button($which) {
 if (&#039;top&#039; === $which) {
     ?&gt;
     &lt;a href=&quot;&lt;?php echo admin_url(&#039;users.php?teacher_user_filter=1&#039;); ?&gt;&quot; class=&quot;button&quot;&gt;篩選老師&lt;/a&gt;
     &lt;?php
 }
}
add_action(&#039;restrict_manage_users&#039;, &#039;ks_add_teacher_user_filter_button&#039;);</code></pre>
</li>
<li>
<p>過濾老師清單：</p>
<pre><code class="lang-php language-php php">function ks_filter_teacher_user_list($query) {
 global $pagenow;

 if (&#039;users.php&#039; === $pagenow &amp;&amp; isset($_GET[&#039;teacher_user_filter&#039;]) &amp;&amp; $_GET[&#039;teacher_user_filter&#039;] === &#039;1&#039;) {
     $meta_query = array(
         array(
             &#039;key&#039; =&gt; &#039;teacher_user&#039;,
             &#039;value&#039; =&gt; &#039;1&#039;,
             &#039;compare&#039; =&gt; &#039;=&#039;
         ),
     );
     $query-&gt;set(&#039;meta_query&#039;, $meta_query);
 }
}
add_action(&#039;pre_get_users&#039;, &#039;ks_filter_teacher_user_list&#039;);</code></pre>
</li>
</ol>
<h2 class="wp-block-heading">實作結果</h2>
<p>完成以上步驟後，使用者編輯頁面會多出「是否是老師」勾選框，使用者列表頁面會顯示老師欄位並可透過篩選按鈕快速查看所有老師帳號，提升管理效率。</p>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-text language-text text">function ks_add_teacher_user_profile($user) {
    ?&gt;

&lt;h3&gt;老師身分設定&lt;/h3&gt;
    &lt;table class=&quot;form-table&quot;&gt;

&lt;tr&gt;

&lt;th&gt;&lt;label for=&quot;teacher_user&quot;&gt;是否為老師&lt;/label&gt;&lt;/th&gt;

&lt;td&gt;
                &lt;input type=&quot;checkbox&quot; name=&quot;teacher_user&quot; id=&quot;teacher_user&quot; value=&quot;1&quot; &lt;?php checked(get_user_meta($user-&gt;ID, &#039;teacher_user&#039;, true), 1); ?&gt; /&gt;
                &lt;span class=&quot;description&quot;&gt;勾選此選項以標記此使用者為老師。&lt;/span&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
    &lt;?php
}
add_action(&#039;show_user_profile&#039;, &#039;ks_add_teacher_user_profile&#039;);
add_action(&#039;edit_user_profile&#039;, &#039;ks_add_teacher_user_profile&#039;);

function save_teacher_user_profile($user_id) {
    if (!current_user_can(&#039;edit_user&#039;, $user_id)) {
        return false;
    }

    $checkbox_value = isset($_POST[&#039;teacher_user&#039;]) ? 1 : 0;
    update_user_meta($user_id, &#039;teacher_user&#039;, $checkbox_value);
}
add_action(&#039;personal_options_update&#039;, &#039;save_teacher_user_profile&#039;);
add_action(&#039;edit_user_profile_update&#039;, &#039;save_teacher_user_profile&#039;);

function ks_add_teacher_user_column($columns) {
    $columns[&#039;teacher_user&#039;] = &#039;老師&#039;;
    return $columns;
}
add_filter(&#039;manage_users_columns&#039;, &#039;ks_add_teacher_user_column&#039;);

function ks_show_teacher_user_column($value, $column_name, $user_id) {
    if (&#039;teacher_user&#039; === $column_name) {
        $is_teacher_user = get_user_meta($user_id, &#039;teacher_user&#039;, true);
        return $is_teacher_user ? &#039;已啟用&#039; : &#039;-&#039;;
    }
    return $value;
}
add_filter(&#039;manage_users_custom_column&#039;, &#039;ks_show_teacher_user_column&#039;, 10, 3);

function ks_add_teacher_user_filter_button($which) {
    if (&#039;top&#039; === $which) {
        ?&gt;
        &lt;a href=&quot;&lt;?php echo admin_url(&#039;users.php?teacher_user_filter=1&#039;); ?&gt;&quot; class=&quot;button&quot;&gt;篩選老師&lt;/a&gt;
        &lt;?php
    }
}
add_action(&#039;restrict_manage_users&#039;, &#039;ks_add_teacher_user_filter_button&#039;);

function ks_filter_teacher_user_list($query) {
    global $pagenow;

    if (&#039;users.php&#039; === $pagenow &amp;&amp; isset($_GET[&#039;teacher_user_filter&#039;]) &amp;&amp; $_GET[&#039;teacher_user_filter&#039;] === &#039;1&#039;) {
        $meta_query = array(
            array(
                &#039;key&#039; =&gt; &#039;teacher_user&#039;,
                &#039;value&#039; =&gt; &#039;1&#039;,
                &#039;compare&#039; =&gt; &#039;=&#039;
            ),
        );
        $query-&gt;set(&#039;meta_query&#039;, $meta_query);
    }
}
add_action(&#039;pre_get_users&#039;, &#039;ks_filter_teacher_user_list&#039;);</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-user-teacher-field/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
