<?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>會員註冊 &#8211; 小豬日常</title>
	<atom:link href="https://piglife.tw/tag/%e6%9c%83%e5%93%a1%e8%a8%bb%e5%86%8a/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 12 Dec 2025 14:52:59 +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>會員註冊 &#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-auto-create-cpt/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-auto-create-cpt/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Thu, 20 Feb 2025 05:45:16 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[acf]]></category>
		<category><![CDATA[user_register]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_insert_post]]></category>
		<category><![CDATA[會員註冊]]></category>
		<category><![CDATA[自訂文章類型]]></category>
		<guid isPermaLink="false">https://piglife.tw/?p=493</guid>

					<description><![CDATA[說明如何在 WordPress 會員註冊時，自動建立自訂文章類型 member_info，並設定預設...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在 WordPress 網站中，常見需求是當新會員註冊時，自動建立一篇專屬於該會員的自訂文章類型（Custom Post Type, CPT），例如會員介紹頁面。本文適合具備基本 WordPress 開發經驗的工程師或自學者，說明如何利用 <code>user_register</code> Hook 自動建立 <code>member_info</code> 文章，並設定內容與 ACF 欄位。</p>
<h2 class="wp-block-heading">註冊 member_info 自訂文章類型</h2>
<p>首先，必須在主題的 <code>functions.php</code> 中註冊 <code>member_info</code> 這個自訂文章類型。以下程式碼會在 WordPress 初始化時執行：</p>
<pre><code class="lang-php language-php php">function register_member_info_post_type() {
  $args = array(
    &#039;labels&#039; =&gt; array(
      &#039;name&#039; =&gt; &#039;會員資訊&#039;,
      &#039;singular_name&#039; =&gt; &#039;會員資訊&#039;
    ),
    &#039;public&#039; =&gt; true,
    &#039;has_archive&#039; =&gt; true,
    &#039;supports&#039; =&gt; array(&#039;title&#039;, &#039;editor&#039;, &#039;author&#039;),
    &#039;rewrite&#039; =&gt; array(&#039;slug&#039; =&gt; &#039;member-info&#039;),
  );
  register_post_type(&#039;member_info&#039;, $args);
}
add_action(&#039;init&#039;, &#039;register_member_info_post_type&#039;);</code></pre>
<p>此段程式碼會建立一個公開且有存檔頁的 CPT，並支援標題、內容編輯器與作者欄位。</p>
<h2 class="wp-block-heading">自動建立會員資訊文章</h2>
<p>接著，利用 <code>user_register</code> Hook 監聽新會員註冊事件，當會員註冊時自動建立一篇 <code>member_info</code> 文章，並填入預設內容與 ACF 欄位：</p>
<pre><code class="lang-php language-php php">function ks_create_member_info_post($user_id) {
  $user = get_userdata($user_id);
  $display_name = $user-&gt;display_name;
  $post_title = $display_name;

  $post_content = &#039;

  &lt;h3 class=&quot;wp-block-heading&quot;&gt;&lt;strong&gt;經歷&lt;/strong&gt;（公開資訊）&lt;/h3&gt;

&lt;p&gt;公開資訊&lt;/p&gt;

  &lt;h3 class=&quot;wp-block-heading&quot;&gt;&lt;strong&gt;專長&lt;/strong&gt;（公開資訊）&lt;/h3&gt;

&lt;p&gt;公開資訊&lt;/p&gt;
  &#039;;

  $post_data = array(
    &#039;post_title&#039; =&gt; $post_title,
    &#039;post_content&#039; =&gt; $post_content,
    &#039;post_status&#039; =&gt; &#039;publish&#039;,
    &#039;post_author&#039; =&gt; $user_id,
    &#039;post_type&#039; =&gt; &#039;member_info&#039;,
  );

  $post_id = wp_insert_post($post_data);

  if ($post_id) {
    update_field(&#039;member_content&#039;, &#039;登入會員限定內容&#039;, $post_id);
    update_field(&#039;member_current_position&#039;, &#039;目前任職&#039;, $post_id);
    update_field(&#039;member_experience&#039;, &#039;（公開資訊區）&lt;br&gt;信箱、網站、學歷、學號？&#039;, $post_id);
  }
}
add_action(&#039;user_register&#039;, &#039;ks_create_member_info_post&#039;);</code></pre>
<p>此函式會取得新會員的顯示名稱作為文章標題，並用 Gutenberg 格式設定文章內容。若有安裝 Advanced Custom Fields，則會填入指定欄位的預設值。</p>
<h2 class="wp-block-heading">程式碼說明</h2>
<ul>
<li><code>user_register</code> Hook：會員註冊時觸發，執行自動建立文章函式。</li>
<li><code>get_userdata($user_id)</code>：取得會員資料。</li>
<li><code>wp_insert_post()</code>：建立自訂文章並回傳文章 ID。</li>
<li>Gutenberg 內容格式：使用區塊編輯器標記語法，方便未來編輯。</li>
<li>ACF 欄位設定：透過 <code>update_field()</code> 填入預設值，需先安裝並啟用 ACF 外掛。</li>
</ul>
<h2 class="wp-block-heading">確保程式運作正確</h2>
<ul>
<li>確認 <code>member_info</code> CPT 已成功註冊並可在後台管理。</li>
<li>確認 Advanced Custom Fields 外掛已啟用，且欄位名稱正確。</li>
<li>測試新會員註冊流程，並檢查「會員資訊」文章是否自動產生。</li>
</ul>
<h2 class="wp-block-heading">延伸應用與常見坑</h2>
<ul>
<li>若需更多欄位，可在 ACF 中擴充並同步更新 <code>update_field()</code>。</li>
<li>若會員資料需同步更新文章，可監聽 <code>profile_update</code> Hook。</li>
<li>注意文章內容格式需符合 Gutenberg 區塊語法，避免前端顯示異常。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-text language-text text">function register_member_info_post_type() {
  $args = array(
    &#039;labels&#039; =&gt; array(
      &#039;name&#039; =&gt; &#039;會員資訊&#039;,
      &#039;singular_name&#039; =&gt; &#039;會員資訊&#039;
    ),
    &#039;public&#039; =&gt; true,
    &#039;has_archive&#039; =&gt; true,
    &#039;supports&#039; =&gt; array(&#039;title&#039;, &#039;editor&#039;, &#039;author&#039;),
    &#039;rewrite&#039; =&gt; array(&#039;slug&#039; =&gt; &#039;member-info&#039;),
  );
  register_post_type(&#039;member_info&#039;, $args);
}
add_action(&#039;init&#039;, &#039;register_member_info_post_type&#039;);

function ks_create_member_info_post($user_id) {
  $user = get_userdata($user_id);
  $display_name = $user-&gt;display_name;
  $post_title = $display_name;

  $post_content = &#039;

  &lt;h3 class=&quot;wp-block-heading&quot;&gt;&lt;strong&gt;經歷&lt;/strong&gt;（公開資訊）&lt;/h3&gt;

&lt;p&gt;公開資訊&lt;/p&gt;

  &lt;h3 class=&quot;wp-block-heading&quot;&gt;&lt;strong&gt;專長&lt;/strong&gt;（公開資訊）&lt;/h3&gt;

&lt;p&gt;公開資訊&lt;/p&gt;
  &#039;;

  $post_data = array(
    &#039;post_title&#039; =&gt; $post_title,
    &#039;post_content&#039; =&gt; $post_content,
    &#039;post_status&#039; =&gt; &#039;publish&#039;,
    &#039;post_author&#039; =&gt; $user_id,
    &#039;post_type&#039; =&gt; &#039;member_info&#039;,
  );

  $post_id = wp_insert_post($post_data);

  if ($post_id) {
    update_field(&#039;member_content&#039;, &#039;登入會員限定內容&#039;, $post_id);
    update_field(&#039;member_current_position&#039;, &#039;目前任職&#039;, $post_id);
    update_field(&#039;member_experience&#039;, &#039;（公開資訊區）&lt;br&gt;信箱、網站、學歷、學號？&#039;, $post_id);
  }
}
add_action(&#039;user_register&#039;, &#039;ks_create_member_info_post&#039;);</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-auto-create-cpt/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
