<?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/category/technical-notes/feed/" rel="self" type="application/rss+xml" />
	<link>https://piglife.tw</link>
	<description>Hello World，一個紀錄生活與學習的地方</description>
	<lastBuildDate>Fri, 02 Jan 2026 22:20:44 +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>使用 Firebase Firestore 匯出抽獎資料為 CSV 檔案的實作說明</title>
		<link>https://piglife.tw/technical-notes/firebase-firestore-csv-export/</link>
					<comments>https://piglife.tw/technical-notes/firebase-firestore-csv-export/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Fri, 02 Jan 2026 22:20:44 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[CSV匯出]]></category>
		<category><![CDATA[Firebase]]></category>
		<category><![CDATA[Firestore]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[前端匯出]]></category>
		<category><![CDATA[資料分頁]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/firebase-firestore-csv-export/</guid>

					<description><![CDATA[本文說明如何結合 Firebase Firestore 與前端 JavaScript 實作抽獎資料的...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>本篇文章介紹如何在 PHP 環境中結合 Firebase Firestore，實作一個前端按鈕匯出抽獎資料為 CSV 檔案的功能。此功能適合需要將 Firestore 資料批次匯出，並提供給非技術人員下載的場景。讀者需具備基本 PHP 與 JavaScript 知識，並了解 Firebase Firestore 的基本操作。</p>
<h2 class="wp-block-heading">Firebase 初始化與匿名登入</h2>
<p>在前端使用 Firebase SDK，首先透過 <code>initializeApp</code> 初始化 Firebase，並使用匿名登入 (<code>signInAnonymously</code>) 取得存取 Firestore 的權限。這樣做可以避免在伺服器端直接操作 Firebase，減少安全風險。</p>
<pre><code class="lang-js language-js js">const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
await signInAnonymously(auth);
const db = getFirestore(app);</code></pre>
<h2 class="wp-block-heading">分頁抓取 Firestore 資料</h2>
<p>Firestore 資料量可能很大，因此使用分頁查詢避免一次讀取過多資料造成效能問題。透過 <code>orderBy(&quot;__name__&quot;)</code> 與 <code>startAfter</code> 搭配 <code>limit</code>，逐頁取得資料直到抓完為止。</p>
<pre><code class="lang-js language-js js">async function fetchAllEntriesPaged(db, pageSize = 500) {
  const colRef = collection(db, ENTRY_COLLECTION_PATH);
  let all = [];
  let lastDoc = null;
  while (true) {
    let q = query(colRef, orderBy(&quot;__name__&quot;), limit(pageSize));
    if (lastDoc) q = query(colRef, orderBy(&quot;__name__&quot;), startAfter(lastDoc), limit(pageSize));
    const snap = await getDocs(q);
    if (snap.empty) break;
    snap.forEach(d =&gt; all.push({ id: d.id, ...d.data() }));
    lastDoc = snap.docs[snap.docs.length - 1];
    if (snap.size &lt; pageSize) break;
  }
  return all;
}</code></pre>
<h2 class="wp-block-heading">CSV 格式處理與下載</h2>
<p>匯出 CSV 需要將資料轉成符合格式的字串，並處理特殊字元（如逗號、雙引號、換行）。<code>csvEscape</code> 函式負責這部分。下載時利用 Blob 物件與 <code>URL.createObjectURL</code> 產生可下載的連結。</p>
<pre><code class="lang-js language-js js">function csvEscape(val) {
  if (val === null || val === undefined) return &quot;&quot;;
  const s = String(val);
  if (/[&quot;,\n\r]/.test(s)) return `&quot;${s.replace(/&quot;/g, &#039;&quot;&quot;&#039;)}&quot;`;
  return s;
}

function downloadCSV(lines, filename) {
  const csv = &quot;\uFEFF&quot; + lines.join(&quot;\r\n&quot;);
  const blob = new Blob([csv], { type: &quot;text/csv;charset=utf-8;&quot; });
  const url = URL.createObjectURL(blob);
  const a = document.createElement(&quot;a&quot;);
  a.href = url;
  a.download = filename;
  document.body.appendChild(a);
  a.click();
  a.remove();
  URL.revokeObjectURL(url);
}</code></pre>
<h2 class="wp-block-heading">時間戳記格式轉換</h2>
<p>Firestore 的時間欄位格式特殊，可能是 Timestamp 物件或其他形式，透過 <code>tsToISO</code> 函式統一轉成 ISO 8601 字串，方便 CSV 讀取與後續處理。</p>
<pre><code class="lang-js language-js js">function tsToISO(v) {
  try {
    if (!v) return &quot;&quot;;
    if (typeof v.toDate === &quot;function&quot;) return v.toDate().toISOString();
    if (typeof v.seconds === &quot;number&quot;) return new Date(v.seconds * 1000).toISOString();
    return String(v);
  } catch {
    return &quot;&quot;;
  }
}</code></pre>
<h2 class="wp-block-heading">實作匯出流程</h2>
<p>按下「下載 CSV」按鈕後，觸發 <code>runExport</code> 函式，依序完成初始化、登入、資料分頁抓取、CSV 組合、下載檔案與狀態更新。過程中會禁用按鈕避免重複點擊，並顯示目前狀態。</p>
<pre><code class="lang-js language-js js">async function runExport() {
  $status.textContent = &quot;&quot;;
  $btn.disabled = true;
  $btn.textContent = &quot;匯出中...&quot;;
  try {
    const app = initializeApp(firebaseConfig);
    const auth = getAuth(app);
    await signInAnonymously(auth);
    const db = getFirestore(app);
    $status.textContent = &quot;正在抓取資料...&quot;;
    const rows = await fetchAllEntriesPaged(db, 500);
    if (!rows.length) {
      $status.textContent = &quot;沒有任何資料可匯出。&quot;;
      return;
    }
    const headers = [&quot;docId&quot;, &quot;name&quot;, &quot;classroom&quot;, &quot;teacher&quot;, &quot;entryDate&quot;, &quot;entryNumber&quot;, &quot;isWinner&quot;, &quot;drawTime&quot;, &quot;drawnBy&quot;, &quot;createdAt&quot;, &quot;updatedAt&quot;];
    const lines = [];
    lines.push(headers.map(csvEscape).join(&quot;,&quot;));
    for (const r of rows) {
      const line = [
        r.id ?? &quot;&quot;,
        r.name ?? &quot;&quot;,
        r.classroom ?? &quot;&quot;,
        r.teacher ?? &quot;&quot;,
        r.entryDate ?? &quot;&quot;,
        r.entryNumber ?? &quot;&quot;,
        (r.isWinner === true) ? &quot;true&quot; : &quot;false&quot;,
        r.drawTime ?? &quot;&quot;,
        r.drawnBy ?? &quot;&quot;,
        tsToISO(r.createdAt),
        tsToISO(r.updatedAt),
      ].map(csvEscape).join(&quot;,&quot;);
      lines.push(line);
    }
    const filename = `christmas_raffle_entries_${getTodayDate()}.csv`;
    downloadCSV(lines, filename);
    $status.textContent = `匯出完成：共 ${rows.length} 筆`;
  } catch (e) {
    console.error(e);
    $status.textContent = `匯出失敗：${e.message}`;
  } finally {
    $btn.disabled = false;
    $btn.textContent = &quot;⬇️ 下載 CSV&quot;;
  }
}</code></pre>
<h2 class="wp-block-heading">實際應用與延伸</h2>
<p>此實作適合用於管理後台快速匯出 Firestore 資料，方便非技術人員下載報表。未來可擴充支援更多資料欄位，或改用後端匯出以保護 Firebase 金鑰安全。也可以加入進度條或分頁匯出功能，提升使用體驗。</p>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>Firebase 配置需正確填寫並允許匿名登入。</li>
<li>Firestore 權限規則需設定允許讀取資料。</li>
<li>大量資料匯出時，注意瀏覽器記憶體限制與執行時間。</li>
<li>CSV 格式需妥善處理特殊字元避免格式錯亂。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">
&lt;?php
function at_christmas_export_menu_page()
{
    ?&gt;
    &lt;div class=&quot;wrap&quot;&gt;

&lt;h1&gt;匯出抽獎資料 CSV&lt;/h1&gt;

&lt;p&gt;
            &lt;button id=&quot;at-export-btn&quot; class=&quot;button button-primary&quot;&gt;⬇️ 下載 CSV&lt;/button&gt;
            &lt;span id=&quot;at-export-status&quot; style=&quot;margin-left:10px;&quot;&gt;&lt;/span&gt;
        &lt;/p&gt;
    &lt;/div&gt;

    &lt;script type=&quot;module&quot;&gt;
        import { initializeApp } from &quot;https://www.gstatic.com/firebasejs/11.6.1/firebase-app.js&quot;;
        import { getAuth, signInAnonymously } from &quot;https://www.gstatic.com/firebasejs/11.6.1/firebase-auth.js&quot;;
        import {
            getFirestore,
            collection,
            query,
            getDocs,
            orderBy,
            limit,
            startAfter
        } from &quot;https://www.gstatic.com/firebasejs/11.6.1/firebase-firestore.js&quot;;

        const firebaseConfig = {
            apiKey: &quot;YOUR_API_KEY&quot;,
            authDomain: &quot;YOUR_AUTH_DOMAIN&quot;,
            projectId: &quot;YOUR_PROJECT_ID&quot;,
            storageBucket: &quot;YOUR_STORAGE_BUCKET&quot;,
            messagingSenderId: &quot;YOUR_MESSAGING_SENDER_ID&quot;,
            appId: &quot;YOUR_APP_ID&quot;,
            measurementId: &quot;YOUR_MEASUREMENT_ID&quot;
        };

        const projectId = firebaseConfig.projectId || &quot;YOUR_PROJECT_ID&quot;;
        const ENTRY_COLLECTION_PATH = `artifacts/${projectId}/public/data/christmas_raffle_entries`;

        const $btn = document.getElementById(&quot;at-export-btn&quot;);
        const $status = document.getElementById(&quot;at-export-status&quot;);

        function getTodayDate() {
            const now = new Date();
            const y = now.getFullYear();
            const m = String(now.getMonth() + 1).padStart(2, &quot;0&quot;);
            const d = String(now.getDate()).padStart(2, &quot;0&quot;);
            return `${y}-${m}-${d}`;
        }

        function csvEscape(val) {
            if (val === null || val === undefined) return &quot;&quot;;
            const s = String(val);
            if (/[&quot;,\n\r]/.test(s)) return `&quot;${s.replace(/&quot;/g, &#039;&quot;&quot;&#039;)}&quot;`;
            return s;
        }

        function tsToISO(v) {
            try {
                if (!v) return &quot;&quot;;
                if (typeof v.toDate === &quot;function&quot;) return v.toDate().toISOString();
                if (typeof v.seconds === &quot;number&quot;) return new Date(v.seconds * 1000).toISOString();
                return String(v);
            } catch {
                return &quot;&quot;;
            }
        }

        async function fetchAllEntriesPaged(db, pageSize = 500) {
            const colRef = collection(db, ENTRY_COLLECTION_PATH);

            let all = [];
            let lastDoc = null;

            while (true) {
                let q = query(colRef, orderBy(&quot;__name__&quot;), limit(pageSize));
                if (lastDoc) q = query(colRef, orderBy(&quot;__name__&quot;), startAfter(lastDoc), limit(pageSize));

                const snap = await getDocs(q);
                if (snap.empty) break;

                snap.forEach(d =&gt; all.push({ id: d.id, ...d.data() }));
                lastDoc = snap.docs[snap.docs.length - 1];

                if (snap.size &lt; pageSize) break;
            }
            return all;
        }

        function downloadCSV(lines, filename) {
            const csv = &quot;\uFEFF&quot; + lines.join(&quot;\r\n&quot;); // BOM for Excel
            const blob = new Blob([csv], { type: &quot;text/csv;charset=utf-8;&quot; });
            const url = URL.createObjectURL(blob);
            const a = document.createElement(&quot;a&quot;);
            a.href = url;
            a.download = filename;
            document.body.appendChild(a);
            a.click();
            a.remove();
            URL.revokeObjectURL(url);
        }

        async function runExport() {
            $status.textContent = &quot;&quot;;
            $btn.disabled = true;
            $btn.textContent = &quot;匯出中...&quot;;

            try {
                const app = initializeApp(firebaseConfig);
                const auth = getAuth(app);
                await signInAnonymously(auth);

                const db = getFirestore(app);

                $status.textContent = &quot;正在抓取資料...&quot;;
                const rows = await fetchAllEntriesPaged(db, 500);

                if (!rows.length) {
                    $status.textContent = &quot;沒有任何資料可匯出。&quot;;
                    return;
                }

                const headers = [
                    &quot;docId&quot;,
                    &quot;name&quot;,
                    &quot;classroom&quot;,
                    &quot;teacher&quot;,
                    &quot;entryDate&quot;,
                    &quot;entryNumber&quot;,
                    &quot;isWinner&quot;,
                    &quot;drawTime&quot;,
                    &quot;drawnBy&quot;,
                    &quot;createdAt&quot;,
                    &quot;updatedAt&quot;
                ];

                const lines = [];
                lines.push(headers.map(csvEscape).join(&quot;,&quot;));

                for (const r of rows) {
                    const line = [
                        r.id ?? &quot;&quot;,
                        r.name ?? &quot;&quot;,
                        r.classroom ?? &quot;&quot;,
                        r.teacher ?? &quot;&quot;,
                        r.entryDate ?? &quot;&quot;,
                        r.entryNumber ?? &quot;&quot;,
                        (r.isWinner === true) ? &quot;true&quot; : &quot;false&quot;,
                        r.drawTime ?? &quot;&quot;,
                        r.drawnBy ?? &quot;&quot;,
                        tsToISO(r.createdAt),
                        tsToISO(r.updatedAt),
                    ].map(csvEscape).join(&quot;,&quot;);
                    lines.push(line);
                }

                const filename = `christmas_raffle_entries_${getTodayDate()}.csv`;
                downloadCSV(lines, filename);

                $status.textContent = `匯出完成：共 ${rows.length} 筆`;
            } catch (e) {
                console.error(e);
                $status.textContent = `匯出失敗：${e.message}`;
            } finally {
                $btn.disabled = false;
                $btn.textContent = &quot;⬇️ 下載 CSV&quot;;
            }
        }

        $btn.addEventListener(&quot;click&quot;, runExport);
    &lt;/script&gt;
&lt;?php
}
?&gt;</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/firebase-firestore-csv-export/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>在 LearnDash 課程列表新增已報名學生數欄位並優化查詢效能</title>
		<link>https://piglife.tw/technical-notes/learndash-enrolled-count-column/</link>
					<comments>https://piglife.tw/technical-notes/learndash-enrolled-count-column/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Thu, 01 Jan 2026 22:20:43 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[LearnDash]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WP_User_Query]]></category>
		<category><![CDATA[後台自訂欄位]]></category>
		<category><![CDATA[快取優化]]></category>
		<category><![CDATA[課程管理]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/learndash-enrolled-count-column/</guid>

					<description><![CDATA[本文介紹如何在 LearnDash 後台課程列表新增已報名學生數欄位，並透過 WP_User_Que...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>這段程式碼主要解決 LearnDash 後台課程列表無法直接查看每門課程已報名學生數的問題。對於管理大量課程與學員的教學平台管理員來說，快速掌握報名狀況非常重要。本文適合熟悉 WordPress 與 LearnDash 且想自訂後台介面與效能優化的工程師或自學者。</p>
<h2 class="wp-block-heading">新增自訂欄位到課程列表</h2>
<p>透過 WordPress 提供的 <code>manage_edit-{$post_type}_columns</code> 過濾器，我們在 sfwd-courses（LearnDash 課程）後台列表中插入一個「已報名學生」欄位。此欄位被放置在標題欄位後面，方便管理者一目了然。</p>
<pre><code class="lang-php language-php php">add_filter(&#039;manage_edit-sfwd-courses_columns&#039;, function ($columns) {
    $new = [];
    foreach ($columns as $k =&gt; $v) {
        $new[$k] = $v;
        if ($k === &#039;title&#039;) {
            $new[&#039;ld_enrolled_count&#039;] = &#039;已報名學生&#039;;
        }
    }
    if (!isset($new[&#039;ld_enrolled_count&#039;])) {
        $new[&#039;ld_enrolled_count&#039;] = &#039;已報名學生&#039;;
    }
    return $new;
}, 20);</code></pre>
<h2 class="wp-block-heading">顯示欄位內容與計數邏輯</h2>
<p>使用 <code>manage_sfwd-courses_posts_custom_column</code> 動作鉤子，根據課程 ID 呼叫自訂函式取得報名學生數並輸出。計數邏輯透過 WP_User<em>Query 查詢所有有 <code>usermeta</code> 中 `course</em>{course_id}_access_from` 欄位存在的使用者數量，這是 LearnDash 判斷學員是否有課程存取權的標準做法。</p>
<pre><code class="lang-php language-php php">add_action(&#039;manage_sfwd-courses_posts_custom_column&#039;, function ($column, $post_id) {
    if ($column !== &#039;ld_enrolled_count&#039;)
        return;

    $count = vs_ld_get_enrolled_count($post_id);

    echo esc_html((string) $count);
}, 10, 2);</code></pre>
<h3 class="wp-block-heading">快取機制提升效能</h3>
<p>為避免課程列表頁面每筆課程都執行重複且昂貴的資料庫查詢，使用 WordPress 內建快取（wp_cache）暫存結果 10 分鐘。這樣可以大幅減少對資料庫的負擔，避免後台卡頓。</p>
<pre><code class="lang-php language-php php">function vs_ld_get_enrolled_count($course_id)
{
    $course_id = (int) $course_id;
    if ($course_id &lt;= 0)
        return 0;

    $cache_key = &#039;course_enrolled_&#039; . $course_id;
    $cached = wp_cache_get($cache_key, &#039;ld_course_enroll_count&#039;);
    if ($cached !== false) {
        return (int) $cached;
    }

    $meta_key = &#039;course_&#039; . $course_id . &#039;_access_from&#039;;

    $uq = new WP_User_Query([
        &#039;fields&#039; =&gt; &#039;ID&#039;,
        &#039;number&#039; =&gt; 1,
        &#039;paged&#039; =&gt; 1,
        &#039;count_total&#039; =&gt; true,
        &#039;meta_query&#039; =&gt; [
            [
                &#039;key&#039; =&gt; $meta_key,
                &#039;compare&#039; =&gt; &#039;EXISTS&#039;,
            ],
        ],
    ]);

    $count = (int) $uq-&gt;get_total();

    wp_cache_set($cache_key, $count, &#039;ld_course_enroll_count&#039;, 10 * MINUTE_IN_SECONDS);

    return $count;
}</code></pre>
<h2 class="wp-block-heading">課程更新時清除快取</h2>
<p>為確保報名數字不會因快取而過時，當課程內容被更新時，會自動刪除對應快取，讓下一次查詢能取得最新資料。</p>
<pre><code class="lang-php language-php php">add_action(&#039;save_post_sfwd-courses&#039;, function ($post_id) {
    if (defined(&#039;DOING_AUTOSAVE&#039;) &amp;&amp; DOING_AUTOSAVE)
        return;
    $post_id = (int) $post_id;
    wp_cache_delete(&#039;course_enrolled_&#039; . $post_id, &#039;ld_course_enroll_count&#039;);
}, 20);</code></pre>
<h2 class="wp-block-heading">實務應用與延伸</h2>
<p>這種方式適合需要在 LearnDash 後台快速掌握各課程報名狀況的管理者，尤其是課程數量龐大時，快取機制可有效減少資料庫負擔。未來可延伸加入排序功能，或結合 AJAX 即時更新報名數，提升使用體驗。</p>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>快取時間設定過短會增加資料庫查詢頻率，過長可能導致數據不即時，需依實際需求調整。</li>
<li>使用 WP_User_Query 查詢大量用戶時，仍需注意資料庫效能，建議搭配適當索引。</li>
<li>本範例假設 LearnDash 使用標準 usermeta key，若有自訂存取邏輯需調整查詢條件。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
if (!defined(&#039;ABSPATH&#039;))
    exit;

/**
 * LearnDash：後台課程清單新增「已報名學生數」欄位
 * - post_type: sfwd-courses
 * - 計數依據：usermeta key = course_{course_id}_access_from EXISTS
 * - 有短暫快取，避免清單頁卡爆
 */

/** 1) 加欄位 */
add_filter(&#039;manage_edit-sfwd-courses_columns&#039;, function ($columns) {
    // 插在標題後面（你也可以改位置）
    $new = [];
    foreach ($columns as $k =&gt; $v) {
        $new[$k] = $v;
        if ($k === &#039;title&#039;) {
            $new[&#039;ld_enrolled_count&#039;] = &#039;已報名學生&#039;;
        }
    }
    if (!isset($new[&#039;ld_enrolled_count&#039;])) {
        $new[&#039;ld_enrolled_count&#039;] = &#039;已報名學生&#039;;
    }
    return $new;
}, 20);

/** 2) 顯示欄位內容 */
add_action(&#039;manage_sfwd-courses_posts_custom_column&#039;, function ($column, $post_id) {
    if ($column !== &#039;ld_enrolled_count&#039;)
        return;

    $count = vs_ld_get_enrolled_count($post_id);

    echo esc_html((string) $count);
}, 10, 2);

/** 3) 計數函式（含快取） */
function vs_ld_get_enrolled_count($course_id)
{
    $course_id = (int) $course_id;
    if ($course_id &lt;= 0)
        return 0;

    // 快取 10 分鐘（避免列表頁每一列都打一次 users table）
    $cache_key = &#039;course_enrolled_&#039; . $course_id;
    $cached = wp_cache_get($cache_key, &#039;ld_course_enroll_count&#039;);
    if ($cached !== false) {
        return (int) $cached;
    }

    // LearnDash 最常用的課程存取 key
    $meta_key = &#039;course_&#039; . $course_id . &#039;_access_from&#039;;

    // 用 WP_User_Query 計數 meta_key EXISTS 的使用者
    $uq = new WP_User_Query([
        &#039;fields&#039; =&gt; &#039;ID&#039;,
        &#039;number&#039; =&gt; 1,        // 只要 count_total，不要真的撈一堆 ID
        &#039;paged&#039; =&gt; 1,
        &#039;count_total&#039; =&gt; true,
        &#039;meta_query&#039; =&gt; [
            [
                &#039;key&#039; =&gt; $meta_key,
                &#039;compare&#039; =&gt; &#039;EXISTS&#039;,
            ],
        ],
    ]);

    $count = (int) $uq-&gt;get_total();

    wp_cache_set($cache_key, $count, &#039;ld_course_enroll_count&#039;, 10 * MINUTE_IN_SECONDS);

    return $count;
}

/** 4) 當課程更新時，清掉該課程快取（避免顯示太久舊數字） */
add_action(&#039;save_post_sfwd-courses&#039;, function ($post_id) {
    if (defined(&#039;DOING_AUTOSAVE&#039;) &amp;&amp; DOING_AUTOSAVE)
        return;
    $post_id = (int) $post_id;
    wp_cache_delete(&#039;course_enrolled_&#039; . $post_id, &#039;ld_course_enroll_count&#039;);
}, 20);</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/learndash-enrolled-count-column/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>JavaScript 實作禁止右鍵選單的簡易方法</title>
		<link>https://piglife.tw/technical-notes/javascript-disable-right-click/</link>
					<comments>https://piglife.tw/technical-notes/javascript-disable-right-click/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Tue, 30 Dec 2025 22:20:29 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[事件監聽]]></category>
		<category><![CDATA[前端防護]]></category>
		<category><![CDATA[右鍵禁用]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/javascript-disable-right-click/</guid>

					<description><![CDATA[介紹如何使用 JavaScript 監聽 contextmenu 事件並阻止預設右鍵選單，提供簡單有...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在某些網頁應用中，開發者可能希望防止使用者透過滑鼠右鍵開啟選單，以避免內容被輕易複製或操作。這段程式碼提供了一個簡單且不影響其他滑鼠功能的解決方案，適合有基礎 JavaScript 知識且想快速實作右鍵禁用的工程師或自學者。</p>
<h2 class="wp-block-heading">事件監聽與右鍵選單阻擋</h2>
<p>核心概念是監聽瀏覽器的 <code>contextmenu</code> 事件，該事件會在使用者嘗試開啟右鍵選單時觸發。透過呼叫 <code>event.preventDefault()</code>，可以阻止預設行為，也就是禁止顯示右鍵選單。</p>
<h3 class="wp-block-heading">重要程式碼片段</h3>
<pre><code class="lang-javascript language-javascript javascript">document.addEventListener(&quot;contextmenu&quot;, function (e) {
    e.preventDefault();
});</code></pre>
<p>這段程式碼綁定全域的 <code>contextmenu</code> 事件監聽器，當事件發生時直接阻止預設動作。</p>
<h2 class="wp-block-heading">為什麼用立即執行函式包裹？</h2>
<p>使用立即執行函式 (IIFE) 可以避免全域變數污染，確保程式碼在嚴格模式下運行 (<code>&quot;use strict&quot;</code>)，增加程式的穩定性與安全性。</p>
<h2 class="wp-block-heading">實務應用與注意事項</h2>
<ul>
<li>此方法只禁止右鍵選單，不會影響左鍵點擊、文字選取或快捷鍵操作，對使用者體驗影響較小。</li>
<li>但此防護並非絕對安全，仍可被瀏覽器開發者工具或其他方式繞過，適合用於降低一般使用者誤操作。</li>
<li>若需更嚴格的內容保護，建議搭配其他前端或後端策略。</li>
</ul>
<h2 class="wp-block-heading">延伸優化方向</h2>
<ul>
<li>可針對特定元素綁定 <code>contextmenu</code> 事件，避免全域禁用造成使用不便。</li>
<li>結合提示訊息告知使用者右鍵功能已被禁用，提高使用者理解。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-javascript language-javascript javascript">(function () {
    &quot;use strict&quot;;

    // 只鎖右鍵選單（不影響左鍵、選字、快捷鍵）
    document.addEventListener(&quot;contextmenu&quot;, function (e) {
        e.preventDefault();
    });
})();</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/javascript-disable-right-click/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress 自訂文章類型批次內容關鍵字取代工具實作</title>
		<link>https://piglife.tw/technical-notes/wordpress-solution-content-replace-2/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-solution-content-replace-2/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Mon, 29 Dec 2025 22:21:12 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[後台擴充]]></category>
		<category><![CDATA[批次替換]]></category>
		<category><![CDATA[自訂文章類型]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/wordpress-solution-content-replace-2/</guid>

					<description><![CDATA[介紹如何在 WordPress 自訂文章類型 solution 的後台新增批次內容關鍵字替換工具，透...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在管理 WordPress 自訂文章類型（Custom Post Type）時，常會遇到需要批次替換文章內容或摘要中的特定字串的需求。手動逐篇修改不僅耗時，也容易出錯。這段程式碼示範如何在 WordPress 後台為名為 solution 的自訂文章類型新增一個子選單，提供一個簡單介面讓管理員輸入舊字串與新字串，並自動搜尋所有 solution 文章的內容與摘要進行批次替換。</p>
<p>這篇文章適合有基本 WordPress 開發經驗，想要了解如何擴充後台功能並操作自訂文章內容的工程師與自學者。</p>
<h2 class="wp-block-heading">新增後台子選單</h2>
<p>使用 <code>add_action(&#039;admin_menu&#039;, ...)</code> 來掛載函式，先判斷 solution 文章類型是否存在，避免錯誤。接著用 <code>add_submenu_page</code> 在 solution 文章列表下新增「內容關鍵字取代」的子選單，權限設定為可編輯文章的用戶。</p>
<pre><code class="lang-php language-php php">add_action(&#039;admin_menu&#039;, function () {
    if (!post_type_exists(&#039;solution&#039;)) {
        return;
    }

    add_submenu_page(
        &#039;edit.php?post_type=solution&#039;,
        &#039;內容關鍵字取代&#039;,
        &#039;內容關鍵字取代&#039;,
        &#039;edit_posts&#039;,
        &#039;solution-content-replace&#039;,
        &#039;solution_content_replace_page&#039;
    );
});</code></pre>
<p>這段程式碼確保只有在 solution 文章類型存在時才新增選單，避免後台出現無效連結。</p>
<h2 class="wp-block-heading">後台頁面與表單設計</h2>
<p><code>solution_content_replace_page</code> 函式負責渲染後台頁面。首先檢查使用者權限，防止未授權存取。頁面包含一個表單，讓使用者輸入「舊字串」與「新字串」，並使用 WordPress 的 nonce 機制防止 CSRF 攻擊。</p>
<pre><code class="lang-php language-php php">if (!current_user_can(&#039;edit_posts&#039;)) {
    wp_die(&#039;沒有權限。&#039;);
}

// 表單中使用 wp_nonce_field 產生安全碼</code></pre>
<p>表單送出後會執行字串替換邏輯。</p>
<h2 class="wp-block-heading">批次搜尋與替換邏輯說明</h2>
<p>當表單送出且 nonce 驗證通過後，程式會取得輸入的舊字串與新字串，並用 <code>WP_Query</code> 撈出所有 solution 文章的 ID。</p>
<pre><code class="lang-php language-php php">$query = new WP_Query([
    &#039;post_type&#039;      =&gt; &#039;solution&#039;,
    &#039;post_status&#039;    =&gt; &#039;any&#039;,
    &#039;posts_per_page&#039; =&gt; -1,
    &#039;fields&#039;         =&gt; &#039;ids&#039;,
]);</code></pre>
<p>接著逐篇文章讀取內容（<code>post_content</code>）與摘要（<code>post_excerpt</code>），利用 <code>mb_strpos</code>（若可用）或 <code>strpos</code> 檢查舊字串是否存在。若存在，則用 <code>str_replace</code> 替換。</p>
<pre><code class="lang-php language-php php">if ($content_pos !== false) {
    $updated_content = str_replace($old, $new, $content);
    if ($updated_content !== $content) {
        $update_args[&#039;post_content&#039;] = $updated_content;
        $status_parts[] = &#039;內容&#039;;
    }
}</code></pre>
<p>替換完成後，只有在內容或摘要確實有變動時才呼叫 <code>wp_update_post</code> 進行更新，避免不必要的資料庫寫入。</p>
<h2 class="wp-block-heading">結果呈現與使用者體驗</h2>
<p>執行完替換後，頁面會顯示替換結果列表，包括文章 ID、標題、替換狀態（內容、摘要或兩者）以及快速編輯連結，方便管理者後續檢查與微調。</p>
<p>若未輸入舊字串，會顯示警告；若找不到符合條件的文章，則提示資訊，提升使用者體驗。</p>
<h2 class="wp-block-heading">實務應用與優化建議</h2>
<p>此工具適合用於需要大量文字修正的場景，例如品牌名稱變更、產品名稱更新或錯字修正。未來可擴充功能，如：</p>
<ul>
<li>支援正規表達式替換</li>
<li>限制替換範圍（例如只替換內容或摘要）</li>
<li>增加替換前預覽功能</li>
<li>加入替換記錄與回滾機制</li>
</ul>
<p>此外，批次更新大量文章時，建議搭配分批處理避免伺服器超時。</p>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>請確認使用者權限設定正確，避免無權限使用此工具。</li>
<li>替換字串為空白時，會將舊字串刪除，請謹慎操作。</li>
<li>使用 <code>mb_strpos</code> 可避免多字節字串判斷錯誤，若環境不支援會退回 <code>strpos</code>。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">
&lt;?php
// 在 solution post type 底下新增子選單：內容關鍵字取代
add_action(&#039;admin_menu&#039;, function () {
    if (!post_type_exists(&#039;solution&#039;)) {
        return;
    }

    add_submenu_page(
        &#039;edit.php?post_type=solution&#039;,
        &#039;內容關鍵字取代&#039;,
        &#039;內容關鍵字取代&#039;,
        &#039;edit_posts&#039;,
        &#039;solution-content-replace&#039;,
        &#039;solution_content_replace_page&#039;
    );
});

// 後台頁面
function solution_content_replace_page() {
    if (!current_user_can(&#039;edit_posts&#039;)) {
        wp_die(&#039;沒有權限。&#039;);
    }

    $old = &#039;&#039;;
    $new = &#039;&#039;;
    $results = [];
    $executed = false;

    if (isset($_POST[&#039;solution_replace_submit&#039;])) {
        check_admin_referer(&#039;solution_replace_action&#039;, &#039;solution_replace_nonce&#039;);

        $old = isset($_POST[&#039;old_keyword&#039;]) ? sanitize_text_field($_POST[&#039;old_keyword&#039;]) : &#039;&#039;;
        $new = isset($_POST[&#039;new_keyword&#039;]) ? sanitize_text_field($_POST[&#039;new_keyword&#039;]) : &#039;&#039;;

        $executed = true;

        if ($old !== &#039;&#039;) {
            $query = new WP_Query([
                &#039;post_type&#039;      =&gt; &#039;solution&#039;,
                &#039;post_status&#039;    =&gt; &#039;any&#039;,
                &#039;posts_per_page&#039; =&gt; -1,
                &#039;fields&#039;         =&gt; &#039;ids&#039;,
            ]);

            foreach ($query-&gt;posts as $post_id) {
                $post = get_post($post_id);
                if (!$post) {
                    continue;
                }

                $content = $post-&gt;post_content;
                $excerpt = $post-&gt;post_excerpt;

                // 檢查舊字串是否存在（內容＋摘要）
                if (function_exists(&#039;mb_strpos&#039;)) {
                    $content_pos = ($content !== &#039;&#039;) ? mb_strpos($content, $old) : false;
                    $excerpt_pos = ($excerpt !== &#039;&#039;) ? mb_strpos($excerpt, $old) : false;
                } else {
                    $content_pos = ($content !== &#039;&#039;) ? strpos($content, $old) : false;
                    $excerpt_pos = ($excerpt !== &#039;&#039;) ? strpos($excerpt, $old) : false;
                }

                if ($content_pos !== false || $excerpt_pos !== false) {
                    $update_args = [
                        &#039;ID&#039; =&gt; $post_id,
                    ];
                    $status_parts = [];

                    // 內容替換
                    if ($content_pos !== false) {
                        $updated_content = str_replace($old, $new, $content);
                        if ($updated_content !== $content) {
                            $update_args[&#039;post_content&#039;] = $updated_content;
                            $status_parts[] = &#039;內容&#039;;
                        }
                    }

                    // 摘要替換
                    if ($excerpt_pos !== false) {
                        $updated_excerpt = str_replace($old, $new, $excerpt);
                        if ($updated_excerpt !== $excerpt) {
                            $update_args[&#039;post_excerpt&#039;] = $updated_excerpt;
                            $status_parts[] = &#039;摘要&#039;;
                        }
                    }

                    // 真的有需要更新才存
                    if (!empty($status_parts)) {
                        wp_update_post($update_args);

                        $results[] = [
                            &#039;id&#039;     =&gt; $post_id,
                            &#039;title&#039;  =&gt; $post-&gt;post_title,
                            &#039;status&#039; =&gt; &#039;已替換：&#039; . implode(&#039;、&#039;, $status_parts),
                        ];
                    }
                }
            }
            wp_reset_postdata();
        }
    }
    ?&gt;

&lt;h1&gt;Solution &ndash; 內容關鍵字取代&lt;/h1&gt;

&lt;p&gt;此工具會掃描所有 &lt;code&gt;solution</code> 文章的內容與摘要，將符合的字串批次替換。</p>

        
            

            <table class="form-table">

<tr>

<th><label for="old_keyword">要搜尋的字（舊字串）</label></th>

<td>
                        &lt;input type=&quot;text&quot; id=&quot;old_keyword&quot; name=&quot;old_keyword&quot; class=&quot;regular-text&quot;
                               value=&quot;"
                               placeholder="例如：舊字串"&gt;
                    </td>
                </tr>

<tr>

<th><label for="new_keyword">替換成（新字串）</label></th>

<td>
                        &lt;input type=&quot;text&quot; id=&quot;new_keyword&quot; name=&quot;new_keyword&quot; class=&quot;regular-text&quot;
                               value=&quot;"
                               placeholder="例如：新字串（可空白）"&gt;
                    </td>
                </tr>
            </table>

            
        

        

<h3>取代結果</h3>

            
                <div class="notice notice-warning"><p>請輸入要搜尋的舊字串。</p></div>

            
                <div class="notice notice-info"><p>沒有任何文章的內容或摘要包含「」。</p></div>

            
                <table class="widefat striped">

<thead>

<tr>

<th>ID</th>

<th>標題</th>

<th>狀態</th>

<th>編輯連結</th>
                    </tr>
                    </thead>

<tbody>
                    

<tr>

<td></td>

<td></td>

<td></td>

<td>&lt;a href=&quot;" class="button button-small"&gt;編輯</a></td>
                        </tr>
                    
                    </tbody>
                </table>
            

        

    &lt;?php
}</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-solution-content-replace-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WooCommerce 後台訂單自訂欄位資料儲存實作說明</title>
		<link>https://piglife.tw/technical-notes/woocommerce-order-meta-save/</link>
					<comments>https://piglife.tw/technical-notes/woocommerce-order-meta-save/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Sun, 28 Dec 2025 22:20:33 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[woocommerce]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[後台欄位儲存]]></category>
		<category><![CDATA[訂單自訂欄位]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/woocommerce-order-meta-save/</guid>

					<description><![CDATA[說明如何在 WooCommerce 後台訂單編輯頁面保存自訂欄位資料，透過安全且兼容新版訂單存儲的方...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在 WooCommerce 訂單後台管理時，常會需要額外新增自訂欄位以收集特定資訊，例如客戶的聯絡電話。這段程式碼示範如何在訂單編輯頁面保存自訂欄位資料，確保資料能正確存入訂單元資料中。適合已熟悉 WooCommerce 且想擴充後台訂單欄位功能的工程師或自學者。</p>
<h2 class="wp-block-heading">為什麼需要自訂欄位儲存機制</h2>
<p>WooCommerce 預設訂單資料結構無法涵蓋所有業務需求，因此經常會透過自訂欄位來擴充。這些欄位必須在後台訂單編輯時能夠被正確讀取與保存，否則資料會遺失。</p>
<h2 class="wp-block-heading">使用 woocommerce_process_shop_order_meta 鉤子</h2>
<p>這個 action 鉤子會在 WooCommerce 處理訂單後台編輯表單時觸發，適合用來攔截並保存自訂欄位資料。</p>
<pre><code class="lang-php language-php php">add_action(&#039;woocommerce_process_shop_order_meta&#039;, function ($order_id) {
    // 權限檢查，避免非授權使用者修改訂單
    if (!current_user_can(&#039;edit_shop_order&#039;, $order_id))
        return;

    $key = &#039;_shipping_phone&#039;; // 自訂欄位名稱

    // 確認表單有送出該欄位
    if (!isset($_POST[$key]))
        return;

    // 清理輸入資料，避免 XSS 或其他注入風險
    $value = wc_clean(wp_unslash($_POST[$key]));

    // 取得訂單物件
    $order = wc_get_order($order_id);
    if (!$order)
        return;

    // 使用 WC_Order API 更新訂單元資料
    $order-&gt;update_meta_data($key, $value);
    $order-&gt;save(); // 必須呼叫 save() 才會寫入資料庫
}, 50);</code></pre>
<h3 class="wp-block-heading">關鍵說明</h3>
<ul>
<li><code>current_user_can</code> 用來確保只有有編輯訂單權限的使用者能執行更新。</li>
<li><code>wc_clean</code> 和 <code>wp_unslash</code> 是 WordPress 與 WooCommerce 提供的安全函式，確保輸入資料安全。</li>
<li>使用 <code>update_meta_data</code> 與 <code>save</code> 是 WooCommerce 推薦的寫入方式，兼容新版 HPOS（高效訂單存儲系統）與舊版 postmeta。</li>
</ul>
<h2 class="wp-block-heading">實務應用與延伸</h2>
<p>此方法可擴充至任何自訂欄位，只要對應修改 <code>$key</code> 與表單名稱即可。實務中，還可搭配後台欄位輸入介面（如使用 <code>woocommerce_admin_order_data_after_billing_address</code> 鉤子）來完整實作自訂欄位的讀寫。</p>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>忘記呼叫 <code>$order-&gt;save()</code> 將導致資料無法寫入。</li>
<li>欄位名稱建議加底線開頭避免與 WooCommerce 原生欄位衝突。</li>
<li>權限檢查不可省略，避免安全問題。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
add_action(&#039;woocommerce_process_shop_order_meta&#039;, function ($order_id) {
    if (!current_user_can(&#039;edit_shop_order&#039;, $order_id))
        return;

    $key = &#039;_shipping_phone&#039;;

    if (!isset($_POST[$key]))
        return;

    $value = wc_clean(wp_unslash($_POST[$key]));

    $order = wc_get_order($order_id);
    if (!$order)
        return;

    $order-&gt;update_meta_data($key, $value);
    $order-&gt;save();
}, 50);</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/woocommerce-order-meta-save/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress 自動將文章 slug 設為文章 ID 的實作技巧</title>
		<link>https://piglife.tw/technical-notes/wordpress-post-slug-id/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-post-slug-id/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Sat, 27 Dec 2025 22:20:37 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[save_post]]></category>
		<category><![CDATA[slug]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[排程文章]]></category>
		<category><![CDATA[文章 ID]]></category>
		<category><![CDATA[網址管理]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/wordpress-post-slug-id/</guid>

					<description><![CDATA[本文介紹如何在 WordPress 中自動將文章 slug 設為文章 ID，避免重複 slug 並確...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在 WordPress 中，文章的 slug（網址別名）預設是根據標題自動產生，或由使用者手動設定。有時候為了簡化網址管理或避免重複 slug，會想將 slug 固定為文章的 ID。這段程式碼示範如何在文章發佈或排程時，自動將 slug 設為文章 ID，適合需要統一網址格式的開發者或網站管理者。</p>
<h2 class="wp-block-heading">為什麼要用文章 ID 當 slug？</h2>
<p>文章 ID 是 WordPress 中每篇文章的唯一識別碼，使用 ID 當 slug 可以避免重複 slug 的問題，也方便在程式中直接以 ID 取用文章，提升網址的穩定性與一致性。</p>
<h2 class="wp-block-heading">程式碼解析</h2>
<h3 class="wp-block-heading">1. 使用 save_post Hook</h3>
<p>程式碼利用 WordPress 的 <code>save_post</code> action，這個鉤子會在文章儲存時觸發，能即時修改文章資料。</p>
<pre><code class="lang-php language-php php">add_action(&#039;save_post&#039;, function ($post_id, $post, $update) {
    // 程式內容
}, 20, 3);</code></pre>
<p>這裡設定優先權為 20，並且接受三個參數：文章 ID、文章物件、是否為更新。</p>
<h3 class="wp-block-heading">2. 基本保護條件</h3>
<p>避免對修訂版本（revision）或自動儲存（autosave）執行，減少不必要的處理。</p>
<pre><code class="lang-php language-php php">if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) return;
if (!is_object($post)) return;</code></pre>
<h3 class="wp-block-heading">3. 限制只對文章類型生效</h3>
<p>確保只對 <code>post</code> 文章類型操作，不影響其他自訂文章類型。</p>
<pre><code class="lang-php language-php php">if ($post-&gt;post_type !== &#039;post&#039;) return;</code></pre>
<h3 class="wp-block-heading">4. 限制文章狀態</h3>
<p>只對「已發佈（publish）」與「排程（future）」的文章改寫 slug，避免草稿或其他狀態執行。</p>
<pre><code class="lang-php language-php php">$allowed_status = [&#039;publish&#039;, &#039;future&#039;];
if (!in_array($post-&gt;post_status, $allowed_status, true)) return;</code></pre>
<h3 class="wp-block-heading">5. 判斷是否已是目標 slug</h3>
<p>如果 slug 已經是文章 ID，則不再重寫，避免重複操作。</p>
<pre><code class="lang-php language-php php">$target_slug = (string) $post_id;
if ($post-&gt;post_name === $target_slug) return;</code></pre>
<h3 class="wp-block-heading">6. 防止無限迴圈</h3>
<p><code>wp_update_post</code> 會再次觸發 <code>save_post</code>，容易造成無限迴圈。使用靜態變數 <code>$running</code> 作為旗標，避免重複執行。</p>
<pre><code class="lang-php language-php php">static $running = false;
if ($running) return;
$running = true;</code></pre>
<h3 class="wp-block-heading">7. 更新 slug</h3>
<p>呼叫 <code>wp_update_post</code> 更新文章的 <code>post_name</code> 欄位為文章 ID。</p>
<pre><code class="lang-php language-php php">wp_update_post([
    &#039;ID&#039;        =&gt; $post_id,
    &#039;post_name&#039; =&gt; $target_slug,
]);
$running = false;</code></pre>
<h2 class="wp-block-heading">實務應用與延伸</h2>
<ul>
<li>適合網站需要簡潔、唯一的網址結構，或是避免標題相同導致 slug 重複的情況。</li>
<li>可擴充支援更多文章類型或狀態，只要調整條件判斷即可。</li>
<li>若需保留原本 slug，可改成在自訂欄位或其他欄位存 ID。</li>
<li>注意排程文章的 slug 會在排程時間前就設定完成。</li>
</ul>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>無限迴圈問題是此類操作常見的陷阱，務必使用旗標避免。</li>
<li>使用文章 ID 當 slug 可能不利 SEO，需評估是否符合網站策略。</li>
<li>若網站有使用快取或重寫規則，更新 slug 後可能需要清除快取。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
/**
 * Post 發佈時固定把 slug 設為文章 ID
 * - 只針對 post（文章）
 * - 避免無限迴圈
 * - 若已是 ID slug 就不重寫
 * - 支援 publish / future（排程）/ 更新時維持
 */

add_action(&#039;save_post&#039;, function ($post_id, $post, $update) {

    // 1) 基本保護
    if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) return;
    if (!is_object($post)) return;

    // 2) 只針對文章 post
    if ($post-&gt;post_type !== &#039;post&#039;) return;

    // 3) 只在這些狀態下固定（你也可以只留 publish）
    $allowed_status = [&#039;publish&#039;, &#039;future&#039;]; // future = 排程
    if (!in_array($post-&gt;post_status, $allowed_status, true)) return;

    // 4) 目標 slug = 文章 ID
    $target_slug = (string) $post_id;

    // 5) 已經是目標 slug 就不做事
    if ($post-&gt;post_name === $target_slug) return;

    // 6) 防止 save_post -&gt; wp_update_post -&gt; save_post 無限迴圈
    static $running = false;
    if ($running) return;
    $running = true;

    // 7) 更新 slug
    wp_update_post([
        &#039;ID&#039;        =&gt; $post_id,
        &#039;post_name&#039; =&gt; $target_slug,
    ]);

    $running = false;

}, 20, 3);</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-post-slug-id/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>修正 YouTube 影片循環播放的 iframe 參數設定技巧</title>
		<link>https://piglife.tw/technical-notes/youtube-iframe-loop-fix/</link>
					<comments>https://piglife.tw/technical-notes/youtube-iframe-loop-fix/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Fri, 26 Dec 2025 22:20:41 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[YouTube]]></category>
		<category><![CDATA[前端優化]]></category>
		<category><![CDATA[影片循環]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/youtube-iframe-loop-fix/</guid>

					<description><![CDATA[本篇介紹如何透過 JavaScript 自動修正 YouTube iframe 的 URL 參數，確...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在網頁中嵌入 YouTube 影片時，若希望影片能夠無限循環播放，通常需要在 iframe 的 URL 中加入特定參數。這段程式碼解決了直接設定 loop 參數無法生效的問題，適合需要動態調整或修正第三方影片嵌入參數的前端工程師與自學者。</p>
<h2 class="wp-block-heading">問題背景與解決方案</h2>
<p>YouTube 的 iframe 影片循環播放並非只靠 <code>loop=1</code> 參數即可生效，還必須同時指定 <code>playlist</code> 參數為影片 ID。這段程式碼會自動偵測所有帶有 <code>youtube-loop</code> 類別的 iframe，並補上正確的 <code>loop</code> 與 <code>playlist</code> 參數。</p>
<h3 class="wp-block-heading">取得並解析 iframe 的 src</h3>
<p>程式碼先取得所有符合條件的 iframe，並將 <code>src</code> 中可能被轉譯的 <code>&amp;amp;</code> 轉回 <code>&amp;amp;</code>，確保 URL 正確解析。</p>
<pre><code class="lang-javascript language-javascript javascript">const rawSrc = iframe.getAttribute(&#039;src&#039;);
const src = rawSrc.replace(/&amp;amp;/g, &#039;&amp;&#039;);</code></pre>
<h3 class="wp-block-heading">取得影片 ID</h3>
<p>透過字串切割方式，從 src 中擷取影片 ID，這是設定 playlist 參數的關鍵。</p>
<pre><code class="lang-javascript language-javascript javascript">const videoId = src.split(&#039;/embed/&#039;)[1]?.split(&#039;?&#039;)[0];</code></pre>
<h3 class="wp-block-heading">設定 loop 與 playlist 參數</h3>
<p>利用 URL 物件操作，設定 <code>loop=1</code> 與 <code>playlist=影片ID</code>，並避免重複設定造成無限迴圈。</p>
<pre><code class="lang-javascript language-javascript javascript">const url = new URL(src);
url.searchParams.set(&#039;loop&#039;, &#039;1&#039;);
url.searchParams.set(&#039;playlist&#039;, videoId);
const newSrc = url.toString();
if (newSrc !== src) iframe.setAttribute(&#039;src&#039;, newSrc);</code></pre>
<h2 class="wp-block-heading">實務應用與優化建議</h2>
<ul>
<li>此方法適合在頁面載入後或動態插入 iframe 後呼叫，確保所有影片都能正確設定循環播放。</li>
<li>透過多次延遲呼叫（setTimeout）處理 builder 或 lazy load 產生的 iframe。</li>
<li>若有大量 iframe，可考慮改用 MutationObserver 監控 DOM 變化，提高效能與即時性。</li>
</ul>
<h2 class="wp-block-heading">常見問題與坑點</h2>
<ul>
<li>直接在 iframe URL 加 <code>loop=1</code> 不加 <code>playlist</code>，影片不會循環。</li>
<li>URL 中的 <code>&amp;amp;</code> 需先轉回 <code>&amp;amp;</code>，否則 URL 解析會錯誤。</li>
<li>避免重複設定 src，否則會造成 iframe 不斷重新載入。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-javascript language-javascript javascript">(function () {
  function fixYouTubeLoop() {
    document.querySelectorAll(&#039;iframe.youtube-loop&#039;).forEach((iframe) =&gt; {
      const rawSrc = iframe.getAttribute(&#039;src&#039;);
      if (!rawSrc) return;

      // 你的 src 可能含有 &amp;amp;，先轉回正常的 &amp;
      const src = rawSrc.replace(/&amp;amp;/g, &#039;&amp;&#039;);

      if (!src.includes(&#039;youtube.com/embed/&#039;)) return;

      const videoId = src.split(&#039;/embed/&#039;)[1]?.split(&#039;?&#039;)[0];
      if (!videoId) return;

      const url = new URL(src);
      url.searchParams.set(&#039;loop&#039;, &#039;1&#039;);
      url.searchParams.set(&#039;playlist&#039;, videoId);

      const newSrc = url.toString();

      // 避免無限重設
      if (newSrc !== src) iframe.setAttribute(&#039;src&#039;, newSrc);
    });
  }

  document.addEventListener(&#039;DOMContentLoaded&#039;, fixYouTubeLoop);

  // 若是 builder / lazy load 後面才插入 iframe，再補一次
  setTimeout(fixYouTubeLoop, 800);
  setTimeout(fixYouTubeLoop, 2000);
})();</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/youtube-iframe-loop-fix/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress Polylang 多語翻譯批次複製工具實作解析</title>
		<link>https://piglife.tw/technical-notes/wordpress-polylang-bulk-copy/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-polylang-bulk-copy/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Wed, 24 Dec 2025 22:21:27 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[acf]]></category>
		<category><![CDATA[Polylang]]></category>
		<category><![CDATA[taxonomy]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[多語翻譯]]></category>
		<category><![CDATA[批次複製]]></category>
		<category><![CDATA[特色圖片]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/wordpress-polylang-bulk-copy/</guid>

					<description><![CDATA[介紹一款基於 Polylang 官方 API 的 WordPress 多語翻譯批次複製工具，支援任意...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在多語網站開發中，使用 Polylang 外掛管理翻譯文章是一種常見做法。但當需要將大量文章從某一語言批次複製到另一語言時，手動操作效率低且容易出錯。這段程式碼提供一個獨立於特定文章類型的批次翻譯複製工具，適合有基礎 WordPress 與 Polylang 使用經驗的工程師或自學者，幫助快速複製文章並保持翻譯關聯。</p>
<h2 class="wp-block-heading">工具功能與設計架構</h2>
<h3 class="wp-block-heading">1. 固定來源語言與多目標語言選擇</h3>
<p>程式碼中定義常數 <code>IR_SOURCE_LANG</code> 為來源語言（此處固定為中文 zh），目標語言則可從下拉選單選擇（預設有英文 en、日文 ja），方便擴充其他語言。</p>
<h3 class="wp-block-heading">2. 獨立後台介面</h3>
<p>透過 <code>add_submenu_page</code> 將工具掛載於 WordPress 後台「工具」選單下，並提供兩種操作模式：</p>
<ul>
<li>單筆測試：指定文章 ID，快速測試複製功能。</li>
<li>批次複製：依選定文章類型，批次複製該語言下所有文章。</li>
</ul>
<h3 class="wp-block-heading">3. 使用 Polylang 官方 API 確保語言設定與翻譯關聯</h3>
<p>核心複製流程依序使用 <code>wp_insert_post</code> 建立新文章，<code>pll_set_post_language</code> 設定語言，最後用 <code>pll_save_post_translations</code> 儲存翻譯群組關聯，確保與 Polylang 外掛的正確整合。</p>
<h2 class="wp-block-heading">核心複製流程解析</h2>
<h3 class="wp-block-heading">Step 1: 確認來源文章與語言</h3>
<p>先取得來源文章，確認文章類型與來源語言是否符合設定，避免誤複製。</p>
<pre><code class="lang-php language-php php">ir_set_lang_official( $source_id, IR_SOURCE_LANG );</code></pre>
<p>這行確保來源文章語言正確。</p>
<h3 class="wp-block-heading">Step 2: 建立新文章</h3>
<p>使用 <code>wp_insert_post</code> 複製文章標題、內容、狀態、作者、日期等基本欄位，確保新文章與原文一致。</p>
<h3 class="wp-block-heading">Step 3: 設定新文章語言</h3>
<p>新文章建立後，設定為目標語言，保持語言一致性。</p>
<h3 class="wp-block-heading">Step 4: 儲存翻譯關聯</h3>
<p>將新文章加入翻譯群組，讓 Polylang 知道這是原文的翻譯版本。</p>
<h3 class="wp-block-heading">Step 5: 複製分類法（Taxonomy）</h3>
<p>複製原文文章所屬的分類與標籤，但排除語言相關的 taxonomy，避免衝突。</p>
<h3 class="wp-block-heading">Step 6: 複製特色圖片</h3>
<p>如果原文有設定特色圖片，將同一張圖片設定給新文章，保持視覺一致性。</p>
<h3 class="wp-block-heading">Step 7: 複製 ACF 自訂欄位</h3>
<p>利用 Advanced Custom Fields (ACF) 官方 API 安全複製所有自訂欄位，避免欄位名稱變動造成錯誤。</p>
<h2 class="wp-block-heading">實務應用與優化建議</h2>
<ul>
<li><strong>資料備份</strong>：批次操作前務必備份資料庫，避免誤操作造成資料遺失。</li>
<li><strong>語言擴充</strong>：可依需求擴充 <code>$allowed_target_langs</code> 陣列，支援更多語言。</li>
<li><strong>效能考量</strong>：批次複製大量文章時，可能造成伺服器負擔，可分批執行或加入排程。</li>
<li><strong>錯誤處理</strong>：目前以回傳日誌方式呈現，可擴充為錯誤通知或記錄檔。</li>
</ul>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>複製後的文章不會自動翻譯內容，僅複製原文內容，需後續人工或機器翻譯。</li>
<li>Polylang API 函式必須存在，否則功能無法使用。</li>
<li>ACF 複製需確保 ACF 外掛已啟用且函式存在。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code>&lt;?php
/**
 * Universal Polylang Bulk Copy Tool (Final Version - FIXED)
 * - 獨立在「工具 &rarr; Bulk Copy Translations」底下（不綁每個 post type）
 * - 任意 Post Type 下拉選單選擇
 * - 來源語言固定 zh
 * - 目標語言下拉選（en / ja，可自行擴充）
 * - 單筆測試 / 批次處理
 * - 已存在翻譯自動跳過
 * - Polylang 官方流程：wp_insert_post + pll_set_post_language + pll_save_post_translations
 * - taxonomy / featured image / ACF 全欄位複製（ACF 安全寫法）
 *

if ( ! defined( &#039;ABSPATH&#039; ) ) {
    exit;
}

define( &#039;IR_SOURCE_LANG&#039;, &#039;zh&#039; ); // 固定來源語言（Polylang code）

/* =========================
 * 共用：設定語言（官方 API）
 * ========================= */
function ir_set_lang_official( $post_id, $lang_code ) {
    if ( function_exists( &#039;pll_set_post_language&#039; ) ) {
        pll_set_post_language( $post_id, $lang_code );
    }
}

/* =========================
 * 後台選單（獨立於 post type）
 * ========================= */
add_action( &#039;admin_menu&#039;, function () {
    add_submenu_page(
        &#039;tools.php&#039;,                         // 掛在「工具」
        &#039;Bulk Copy Translations (Official)&#039;, // 頁面標題
        &#039;Bulk Copy Translations&#039;,            // 左側選單名稱
        &#039;manage_options&#039;,                    // 權限
        &#039;ir-bulk-translations&#039;,              // slug
        &#039;ir_bulk_translations_page&#039;          // callback
    );
});

/* =========================
 * 後台頁面
 * ========================= */
function ir_bulk_translations_page() {

    if ( ! current_user_can( &#039;manage_options&#039; ) ) {
        wp_die( &#039;沒有權限。&#039; );
    }

    // Polylang API 檢查
    if ( ! function_exists( &#039;pll_set_post_language&#039; ) || ! function_exists( &#039;pll_save_post_translations&#039; ) ) {
        echo &#039;&lt;div class=&quot;notice notice-error&quot;&gt;&lt;p&gt;Polylang API 不存在，請確認 Polylang 是否啟用。&lt;/p&gt;&lt;/div&gt;&#039;;
        return;
    }

    // 允許的目標語言（依照 Polylang 的 code）
    $allowed_target_langs = [ &#039;en&#039;, &#039;ja&#039; ];

    // 可選 post types（public）
    $post_types = get_post_types( [ &#039;public&#039; =&gt; true ], &#039;objects&#039; );

    // 預設值
    $current_post_type = isset( $_POST[&#039;ir_post_type&#039;] ) ? sanitize_text_field( wp_unslash( $_POST[&#039;ir_post_type&#039;] ) ) : &#039;post&#039;;
    if ( ! isset( $post_types[ $current_post_type ] ) ) {
        $current_post_type = &#039;post&#039;;
    }

    $current_target = isset( $_POST[&#039;ir_target_lang&#039;] ) ? sanitize_text_field( wp_unslash( $_POST[&#039;ir_target_lang&#039;] ) ) : &#039;en&#039;;
    if ( ! in_array( $current_target, $allowed_target_langs, true ) ) {
        $current_target = &#039;en&#039;;
    }

    echo &#039;
&lt;h1&gt;通用翻譯複製工具（Polylang 官方流程）&lt;/h1&gt;&#039;;
    echo &#039;
&lt;p&gt;來源語言固定為：&lt;code&gt;&#039; . esc_html( IR_SOURCE_LANG ) . &#039;</code></p>';
    echo '<p style="color:#b32d2e"><strong>⚠️ 執行前請先備份資料庫</strong></p>';

    echo '

        .ir-box{background:#fff;border:1px solid #ccd0d4;padding:12px 14px;margin:12px 0;}
        .ir-row{margin:10px 0;}
        .ir-row label{display:inline-block;min-width:90px;font-weight:600;}
        .ir-pre{background:#fff;border:1px solid #ccd0d4;padding:10px;max-height:520px;overflow:auto;white-space:pre-wrap;}
    ';

    /* =========================
     * 單筆測試
     * ========================= */
    echo '<div class="ir-box">';
    echo '<div style="margin-top:0">單筆測試</div>';
    echo '';
    wp_nonce_field( 'ir_single' );

    echo '<div class="ir-row"><label>Post Type</label> ';
    echo '';
    foreach ( $post_types as $pt ) {
        printf(
            '%s (%s)',
            esc_attr( $pt-&gt;name ),
            selected( $current_post_type, $pt-&gt;name, false ),
            esc_html( $pt-&gt;labels-&gt;singular_name ),
            esc_html( $pt-&gt;name )
        );
    }
    echo '</div>';

    echo '<div class="ir-row"><label>文章 ID</label> ';
    echo '<input type="number" name="ir_test_id" required></div>';

    echo '<div class="ir-row"><label>目標語言</label> ';
    echo '';
    foreach ( $allowed_target_langs as $lang ) {
        printf(
            '%s',
            esc_attr( $lang ),
            selected( $current_target, $lang, false ),
            esc_html( strtoupper( $lang ) )
        );
    }
    echo '</div>';

    submit_button( '測試複製', 'secondary', 'ir_single_run' );
    echo '';
    echo '</div>';

    /* =========================
     * 批次處理
     * ========================= */
    echo '<div class="ir-box">';
    echo '<div style="margin-top:0">批次複製（來源語言：' . esc_html( IR_SOURCE_LANG ) . '）</div>';
    echo '';
    wp_nonce_field( 'ir_bulk' );

    echo '<div class="ir-row"><label>Post Type</label> ';
    echo '';
    foreach ( $post_types as $pt ) {
        printf(
            '%s (%s)',
            esc_attr( $pt-&gt;name ),
            selected( $current_post_type, $pt-&gt;name, false ),
            esc_html( $pt-&gt;labels-&gt;singular_name ),
            esc_html( $pt-&gt;name )
        );
    }
    echo '</div>';

    echo '<div class="ir-row"><label>目標語言</label> ';
    echo '';
    foreach ( $allowed_target_langs as $lang ) {
        printf(
            '%s',
            esc_attr( $lang ),
            selected( $current_target, $lang, false ),
            esc_html( strtoupper( $lang ) )
        );
    }
    echo '</div>';

    submit_button( '執行批次複製', 'primary', 'ir_bulk_run' );
    echo '';
    echo '</div>';

    /* =========================
     * 執行
     * ========================= */
    if ( isset( $_POST['ir_single_run'] ) ) {
        check_admin_referer( 'ir_single' );

        $post_id   = isset( $_POST['ir_test_id'] ) ? absint( $_POST['ir_test_id'] ) : 0;
        $target    = $current_target;
        $post_type = $current_post_type;

        echo '
<div>單筆結果</div>';
        $log = ir_clone_post( $post_id, $target, $post_type );

        echo '<pre class="ir-pre">' . esc_html( implode( "\n", $log ) ) . '</pre>';
    }

    if ( isset( $_POST['ir_bulk_run'] ) ) {
        check_admin_referer( 'ir_bulk' );

        $target    = $current_target;
        $post_type = $current_post_type;

        echo '
<div>批次結果（Post Type：' . esc_html( $post_type ) . '，目標語言：' . esc_html( strtoupper( $target ) ) . '）</div>';

        $posts = get_posts( [
            'post_type'      =&gt; $post_type,
            'posts_per_page' =&gt; -1,
            'post_status'    =&gt; 'any',
            'lang'           =&gt; IR_SOURCE_LANG, // Polylang 的語言 query var
            'fields'         =&gt; 'ids',
        ] );

        if ( empty( $posts ) ) {
            echo '
<p>找不到來源語言（' . esc_html( IR_SOURCE_LANG ) . '）的文章。</p>';
        } else {
            $output = [];
            foreach ( $posts as $id ) {
                $output = array_merge( $output, ir_clone_post( $id, $target, $post_type ) );
            }
            echo '<pre class="ir-pre">' . esc_html( implode( "\n", $output ) ) . '</pre>';
        }
    }

}

/* =========================
 * 核心：複製一篇文章 → 目標語言翻譯
 * ========================= */
function ir_clone_post( $source_id, $target_lang, $post_type ) {

    $log = [];

    if ( ! $source_id ) {
        return [ '❌ 文章 ID 不可為 0' ];
    }

    $src = get_post( $source_id );
    if ( ! $src ) {
        return [ "❌ 找不到文章 {$source_id}" ];
    }

    // 確保來源文章屬於選定的 post type（避免拿錯）
    if ( $src-&gt;post_type !== $post_type ) {
        return [ "❌ 文章 {$source_id} 的 post_type 是 {$src-&gt;post_type}，不是你選的 {$post_type}，已中止。" ];
    }

    // Step 1) 確保原文語言
    ir_set_lang_official( $source_id, IR_SOURCE_LANG );
    $log[] = "Step 1) 設定原文語言：{$source_id} → " . IR_SOURCE_LANG;

    // 取得翻譯群組
    $translations = function_exists( 'pll_get_post_translations' )
        ? pll_get_post_translations( $source_id )
        : [];

    if ( empty( $translations[ IR_SOURCE_LANG ] ) ) {
        $translations[ IR_SOURCE_LANG ] = $source_id;
    }

    // 已存在目標語言 → 跳過
    if ( ! empty( $translations[ $target_lang ] ) ) {
        $existing = (int) $translations[ $target_lang ];
        $log[] = "⚠️ 已存在 {$target_lang} 翻譯（ID {$existing}），跳過。";

        // debug 狀態
        if ( function_exists( 'pll_get_post_language' ) ) {
            $lang_debug = [];
            foreach ( $translations as $code =&gt; $pid ) {
                $lang_debug[] = $code . ':' . $pid . '(' . pll_get_post_language( $pid ) . ')';
            }
            $log[] = '🧪 目前語言狀態：' . implode( ', ', $lang_debug );
        }

        return $log;
    }

    // Step 2) 建立新文章
    $new_id = wp_insert_post( [
        'post_type'      =&gt; $post_type,
        'post_status'    =&gt; $src-&gt;post_status,
        'post_title'     =&gt; $src-&gt;post_title,
        'post_content'   =&gt; $src-&gt;post_content,
        'post_excerpt'   =&gt; $src-&gt;post_excerpt,
        'post_author'    =&gt; $src-&gt;post_author,
        'post_date'      =&gt; $src-&gt;post_date,
        'post_date_gmt'  =&gt; $src-&gt;post_date_gmt,
        'menu_order'     =&gt; $src-&gt;menu_order,
        'post_parent'    =&gt; $src-&gt;post_parent,
    ], true );

    if ( is_wp_error( $new_id ) ) {
        $log[] = '❌ wp_insert_post 錯誤：' . $new_id-&gt;get_error_message();
        return $log;
    }
    $log[] = "Step 2) 建立新文章：{$new_id}";

    // Step 3) 設定新文章語言
    ir_set_lang_official( $new_id, $target_lang );
    $log[] = "Step 3) 設定新文章語言：{$new_id} → {$target_lang}";

    // Step 4) 儲存翻譯關聯
    $translations[ $target_lang ] = $new_id;
    pll_save_post_translations( $translations );
    $log[] = 'Step 4) 儲存翻譯關聯：' . wp_json_encode( $translations );

    // Step 5) 複製 taxonomy（排除語言 taxonomy）
    $taxes = get_object_taxonomies( $post_type );
    $taxes = array_diff( $taxes, [ 'language', 'pll_language' ] );

    foreach ( $taxes as $tax ) {
        $terms = wp_get_object_terms( $source_id, $tax, [ 'fields' =&gt; 'ids' ] );
        if ( ! is_wp_error( $terms ) ) {
            wp_set_object_terms( $new_id, $terms, $tax, false );
        }
    }
    $log[] = 'Step 5) Taxonomy 複製完成';

    // Step 6) 複製特色圖片
    $thumb = get_post_thumbnail_id( $source_id );
    if ( $thumb ) {
        set_post_thumbnail( $new_id, $thumb );
        $log[] = 'Step 6) Featured Image 複製完成';
    } else {
        $log[] = 'Step 6) 原文沒有 Featured Image';
    }

    // Step 7) 複製 ACF 全欄位（官方安全寫法）
    if ( function_exists( 'get_field_objects' ) &amp;&amp; function_exists( 'update_field' ) ) {
        $fields = get_field_objects( $source_id );
        $count  = 0;

        if ( $fields ) {
            foreach ( $fields as $field ) {
                // 用 field key 複製最穩（不怕改欄位 name）
                update_field( $field['key'], $field['value'], $new_id );
                $count++;
            }
        }

        $log[] = "Step 7) ACF 全欄位複製完成（{$count} 個欄位）";
    } else {
        $log[] = 'Step 7) ACF API 不存在（get_field_objects / update_field），跳過 ACF 複製';
    }

    // 最後：語言檢查
    if ( function_exists( 'pll_get_post_language' ) ) {
        $src_lang_real = pll_get_post_language( $source_id );
        $new_lang_real = pll_get_post_language( $new_id );
        $log[] = "🧪 實際語言檢查：原文 {$source_id} 語言：{$src_lang_real}；新文 {$new_id} 語言：{$new_lang_real}";
    }

    $log[] = "✅ 完成：原文 {$source_id} → {$target_lang} 翻譯 {$new_id}";
    return $log;
}</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-polylang-bulk-copy/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WooCommerce 後台商品列表新增複製購物車連結並支援優惠券輸入功能</title>
		<link>https://piglife.tw/technical-notes/woocommerce-copy-cart-link-coupon/</link>
					<comments>https://piglife.tw/technical-notes/woocommerce-copy-cart-link-coupon/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Tue, 23 Dec 2025 22:21:03 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[woocommerce]]></category>
		<category><![CDATA[優惠券]]></category>
		<category><![CDATA[剪貼簿操作]]></category>
		<category><![CDATA[後台自訂欄位]]></category>
		<category><![CDATA[複製連結]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/woocommerce-copy-cart-link-coupon/</guid>

					<description><![CDATA[介紹如何在 WooCommerce 後台商品列表新增一欄複製加入購物車連結的按鈕，並支援彈窗輸入優惠...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在 WooCommerce 後台商品列表中，管理者常需要快速取得商品的「加入購物車」連結以便分享或推廣。這段程式碼實作了在商品列表中新增一欄「複製課程連結」按鈕，點擊後可彈出視窗讓使用者輸入優惠券代碼，並將優惠券參數附加到連結中，最後將完整連結複製到剪貼簿。這對於需要快速產生帶優惠券的購物車連結的電商管理者非常實用。</p>
<h2 class="wp-block-heading">新增自訂欄位顯示複製按鈕</h2>
<p>利用 <code>manage_edit-product_columns</code> 過濾器新增一欄「複製課程連結」，並透過 <code>manage_product_posts_custom_column</code> 動作在該欄位輸出一個帶有商品加入購物車 URL 的按鈕。URL 是以網站首頁 URL 加上 <code>/cart/?add-to-cart=商品ID</code> 組成。</p>
<pre><code class="lang-php language-php php">add_filter(&#039;manage_edit-product_columns&#039;, &#039;add_copy_link_column&#039;);
function add_copy_link_column($columns) {
    $columns[&#039;copy_add_to_cart&#039;] = __(&#039;複製課程連結&#039;, &#039;woocommerce&#039;);
    return $columns;
}

add_action(&#039;manage_product_posts_custom_column&#039;, &#039;show_copy_link_column_content&#039;, 10, 2);
function show_copy_link_column_content($column, $post_id) {
    if ($column === &#039;copy_add_to_cart&#039;) {
        $product_id = (int) $post_id;
        $site_url = my_copy_cart_site_url();
        $add_to_cart_url = $site_url . &#039;/cart/?add-to-cart=&#039; . $product_id;
        echo &#039;&lt;button type=&quot;button&quot; class=&quot;button copy-cart-link&quot; data-url=&quot;&#039; . esc_attr($add_to_cart_url) . &#039;&quot; title=&quot;點擊複製連結&quot;&gt;複製&lt;/button&gt;&#039;;
    }
}</code></pre>
<h2 class="wp-block-heading">客製化 JavaScript 處理複製邏輯與優惠券輸入</h2>
<p>在後台商品列表頁尾插入 JavaScript 和 CSS，當使用者點擊「複製」按鈕時：</p>
<ol>
<li>透過 <code>window.prompt</code> 詢問是否輸入優惠券代碼，可留空。</li>
<li>根據輸入結果組合最終 URL，若有優惠券則加上 <code>&amp;wt_coupon=優惠券代碼</code>。</li>
<li>使用 Clipboard API 複製連結，若不支援則使用傳統 <code>execCommand</code> 備援。</li>
<li>複製成功會顯示成功提示，失敗則顯示錯誤提示並提供手動複製。</li>
</ol>
<pre><code class="lang-js language-js js">function buildFinalUrl(baseUrl) {
    var coupon = window.prompt(&#039;是否要加入優惠券？\n(可留空，直接按確定即可)&#039;, &#039;&#039;);
    if (coupon === null) return baseUrl;
    coupon = String(coupon || &#039;&#039;).trim();
    if (!coupon) return baseUrl;
    var joiner = (baseUrl.indexOf(&#039;?&#039;) === -1) ? &#039;?&#039; : &#039;&amp;&#039;;
    return baseUrl + joiner + &#039;wt_coupon=&#039; + encodeURIComponent(coupon);
}

$(document).on(&#039;click&#039;, &#039;.copy-cart-link&#039;, function(e) {
    e.preventDefault();
    var button = $(this);
    var baseUrl = button.data(&#039;url&#039;);
    var originalHtml = button.html();
    if (button.hasClass(&#039;copying&#039;)) return;
    button.addClass(&#039;copying&#039;);
    var finalUrl = buildFinalUrl(baseUrl);
    if (navigator.clipboard &amp;&amp; window.isSecureContext) {
        navigator.clipboard.writeText(finalUrl).then(function() {
            showCopySuccess(button, originalHtml);
        }).catch(function() {
            fallbackCopyTextToClipboard(finalUrl, button, originalHtml);
        });
    } else {
        fallbackCopyTextToClipboard(finalUrl, button, originalHtml);
    }
});</code></pre>
<h2 class="wp-block-heading">快速動作連結整合</h2>
<p>除了欄位按鈕外，也在商品標題下方的快速動作連結加入「複製購物車連結」，提升操作便利性。此連結同樣帶有商品加入購物車的 URL，並套用相同的 JavaScript 行為。</p>
<pre><code class="lang-php language-php php">add_filter(&#039;post_row_actions&#039;, &#039;add_copy_link_quick_action&#039;, 10, 2);
function add_copy_link_quick_action($actions, $post) {
    if ($post-&gt;post_type === &#039;product&#039;) {
        $product_id = (int) $post-&gt;ID;
        $site_url = my_copy_cart_site_url();
        $add_to_cart_url = $site_url . &#039;/cart/?add-to-cart=&#039; . $product_id;
        $actions[&#039;copy_cart_link&#039;] = &#039;&lt;a href=&quot;#&quot; class=&quot;copy-cart-link&quot; data-url=&quot;&#039; . esc_attr($add_to_cart_url) . &#039;&quot; style=&quot;color:#0073aa;&quot;&gt;📋 複製購物車連結&lt;/a&gt;&#039;;
    }
    return $actions;
}</code></pre>
<h2 class="wp-block-heading">實務應用與優化建議</h2>
<p>此功能適合需要快速產生帶優惠券的購物車連結的 WooCommerce 商店管理員，方便推廣或客服回覆。未來可優化：</p>
<ul>
<li>將網站 URL 改為動態取得，避免硬編碼。</li>
<li>增加複製成功的視覺動畫提升 UX。</li>
<li>支援多種優惠券參數或其他自訂參數。</li>
<li>對於大量商品列表，可考慮優化前端效能。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
/**
 * WooCommerce 後台商品列表添加複製 add-to-cart 連結的功能 + 可選優惠券
 * 適用於 WPCode（PHP Snippet / Run Everywhere 或 Admin Only）
 *
 * 功能：
 * - 商品列表新增一欄「複製課程連結」
 * - 點按後彈出視窗詢問優惠券（可留空）
 * - 若有填，複製的 URL 會加上：&amp;wt_coupon=COUPON
 * - 同時支援「快速動作」複製
 */

/** 你網站的網域（建議改成動態，不用寫死） */
function my_copy_cart_site_url() {
    return home_url();
}

// 在商品列表中添加自定義列
add_filter(&#039;manage_edit-product_columns&#039;, &#039;add_copy_link_column&#039;);
function add_copy_link_column($columns) {
    $columns[&#039;copy_add_to_cart&#039;] = __(&#039;複製課程連結&#039;, &#039;woocommerce&#039;);
    return $columns;
}

// 顯示列內容
add_action(&#039;manage_product_posts_custom_column&#039;, &#039;show_copy_link_column_content&#039;, 10, 2);
function show_copy_link_column_content($column, $post_id) {
    if ($column === &#039;copy_add_to_cart&#039;) {
        $product_id = (int) $post_id;

        $site_url = my_copy_cart_site_url();
        $add_to_cart_url = $site_url . &#039;/cart/?add-to-cart=&#039; . $product_id;

        echo &#039;&lt;button type=&quot;button&quot; class=&quot;button copy-cart-link&quot; data-url=&quot;&#039; . esc_attr($add_to_cart_url) . &#039;&quot; title=&quot;點擊複製連結&quot;&gt;
                 複製
              &lt;/button&gt;&#039;;
    }
}

// 添加 JavaScript 和 CSS
add_action(&#039;admin_footer&#039;, &#039;copy_link_admin_script&#039;);
function copy_link_admin_script() {
    $screen = get_current_screen();
    if (!$screen || $screen-&gt;id !== &#039;edit-product&#039;) {
        return;
    }
    ?&gt;

&lt;style&gt;
        .copy-cart-link {
            display: inline-flex;
            align-items: center;
            gap: 4px;
            padding: 4px 8px;
            font-size: 12px;
            line-height: 1.4;
            border-radius: 3px;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        .copy-cart-link:hover {
            background-color: #000000;
            color: white;
            transform: translateY(-1px);
        }
        .copy-cart-link .dashicons {
            font-size: 14px;
            width: 14px;
            height: 14px;
        }
        .column-copy_add_to_cart {
            width: 110px;
            text-align: center;
        }
        @media screen and (max-width: 782px) {
            .copy-cart-link {
                padding: 6px 10px;
                font-size: 13px;
            }
        }
    &lt;/style&gt;

    &lt;script type=&quot;text/javascript&quot;&gt;
        jQuery(document).ready(function($) {

            // 組裝最終要複製的 URL（可選 wt_coupon）
            function buildFinalUrl(baseUrl) {
                // 1) 詢問優惠券（可留空）
                var coupon = window.prompt(&#039;是否要加入優惠券？\n(可留空，直接按確定即可)&#039;, &#039;&#039;);

                // 使用者按取消 -&gt; 仍照原本複製（你也可以改成 return null 代表不複製）
                if (coupon === null) {
                    return baseUrl;
                }

                coupon = String(coupon || &#039;&#039;).trim();
                if (!coupon) {
                    return baseUrl;
                }

                // 2) 已有 ?add-to-cart=...，直接加 &amp;wt_coupon=
                //    若未來 baseUrl 可能沒有 query，這裡也做保險處理
                var joiner = (baseUrl.indexOf(&#039;?&#039;) === -1) ? &#039;?&#039; : &#039;&amp;&#039;;
                return baseUrl + joiner + &#039;wt_coupon=&#039; + encodeURIComponent(coupon);
            }

            $(document).on(&#039;click&#039;, &#039;.copy-cart-link&#039;, function(e) {
                e.preventDefault();

                var button = $(this);
                var baseUrl = button.data(&#039;url&#039;);
                var originalHtml = button.html();

                if (button.hasClass(&#039;copying&#039;)) return;
                button.addClass(&#039;copying&#039;);

                var finalUrl = buildFinalUrl(baseUrl);

                // 若你希望「按取消就不複製」，把 buildFinalUrl 的取消行為改成 return null
                // 然後這裡加上：
                // if (!finalUrl) { button.removeClass(&#039;copying&#039;); return; }

                if (navigator.clipboard &amp;&amp; window.isSecureContext) {
                    navigator.clipboard.writeText(finalUrl).then(function() {
                        showCopySuccess(button, originalHtml);
                    }).catch(function(err) {
                        console.error(&#039;Copy failed: &#039;, err);
                        fallbackCopyTextToClipboard(finalUrl, button, originalHtml);
                    });
                } else {
                    fallbackCopyTextToClipboard(finalUrl, button, originalHtml);
                }
            });

            function fallbackCopyTextToClipboard(text, button, originalHtml) {
                var textArea = document.createElement(&quot;textarea&quot;);
                textArea.value = text;

                textArea.style.top = &quot;0&quot;;
                textArea.style.left = &quot;0&quot;;
                textArea.style.position = &quot;fixed&quot;;
                textArea.style.opacity = &quot;0&quot;;
                textArea.style.pointerEvents = &quot;none&quot;;

                document.body.appendChild(textArea);
                textArea.focus();
                textArea.select();

                try {
                    var successful = document.execCommand(&#039;copy&#039;);
                    if (successful) {
                        showCopySuccess(button, originalHtml);
                    } else {
                        showCopyError(button, originalHtml, text);
                    }
                } catch (err) {
                    console.error(&#039;Copy failed: &#039;, err);
                    showCopyError(button, originalHtml, text);
                }

                document.body.removeChild(textArea);
            }

            function showCopySuccess(button, originalHtml) {
                button.removeClass(&#039;copying&#039;).addClass(&#039;copied&#039;);
                button.html(&#039;&lt;span class=&quot;dashicons dashicons-yes&quot;&gt;&lt;/span&gt; 已複製&#039;);

                setTimeout(function() {
                    button.removeClass(&#039;copied&#039;);
                    button.html(originalHtml);
                }, 2500);
            }

            function showCopyError(button, originalHtml, text) {
                button.removeClass(&#039;copying&#039;);

                var tooltip = $(&#039;&lt;div class=&quot;copy-error-tooltip&quot; style=&quot;position:absolute;background:#333;color:#fff;padding:8px 12px;border-radius:4px;font-size:12px;z-index:9999;max-width:320px;word-break:break-all;&quot;&gt;複製失敗，請手動複製：&lt;br&gt;&#039; + text + &#039;&lt;/div&gt;&#039;);
                $(&#039;body&#039;).append(tooltip);

                var buttonOffset = button.offset();
                tooltip.css({
                    top: buttonOffset.top - tooltip.outerHeight() - 5,
                    left: buttonOffset.left
                });

                setTimeout(function() { tooltip.remove(); }, 5000);

                tooltip.on(&#039;click&#039;, function() {
                    if (navigator.clipboard) navigator.clipboard.writeText(text);
                    $(this).remove();
                });
            }

        });
    &lt;/script&gt;
    &lt;?php
}

// 可選：添加快速動作連結（在商品名稱下方）
add_filter(&#039;post_row_actions&#039;, &#039;add_copy_link_quick_action&#039;, 10, 2);
function add_copy_link_quick_action($actions, $post) {
    if ($post-&gt;post_type === &#039;product&#039;) {
        $product_id = (int) $post-&gt;ID;

        $site_url = my_copy_cart_site_url();
        $add_to_cart_url = $site_url . &#039;/cart/?add-to-cart=&#039; . $product_id;

        $actions[&#039;copy_cart_link&#039;] = &#039;&lt;a href=&quot;#&quot; class=&quot;copy-cart-link&quot; data-url=&quot;&#039; . esc_attr($add_to_cart_url) . &#039;&quot; style=&quot;color:#0073aa;&quot;&gt;📋 複製購物車連結&lt;/a&gt;&#039;;
    }
    return $actions;
}
</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/woocommerce-copy-cart-link-coupon/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress 後台為自訂文章類型新增內容關鍵字搜尋功能</title>
		<link>https://piglife.tw/technical-notes/wordpress-solution-content-search/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-solution-content-search/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Mon, 22 Dec 2025 22:20:38 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_query]]></category>
		<category><![CDATA[內容關鍵字搜尋]]></category>
		<category><![CDATA[後台搜尋]]></category>
		<category><![CDATA[自訂文章類型]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/wordpress-solution-content-search/</guid>

					<description><![CDATA[介紹如何在 WordPress 自訂文章類型 solution 的後台新增內容關鍵字搜尋功能，透過 ...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在 WordPress 後台管理自訂文章類型（此處為 solution）時，若想快速搜尋文章內容中是否包含特定關鍵字，系統預設的搜尋功能可能無法精準或方便達成此需求。這段程式碼示範如何為 solution 文章類型新增一個後台子選單，提供內容關鍵字搜尋功能，適合需要管理大量文章並快速定位內容的開發者與網站管理員。</p>
<h2 class="wp-block-heading">新增後台子選單</h2>
<p>使用 <code>admin_menu</code> action，先檢查 solution 文章類型是否存在，避免錯誤。接著透過 <code>add_submenu_page</code> 在 solution 列表底下新增「內容關鍵字搜尋」子選單，設定權限為 <code>edit_posts</code>，確保只有具備編輯權限的使用者能操作。</p>
<pre><code class="lang-php language-php php">add_action( &#039;admin_menu&#039;, function () {
    if ( ! post_type_exists( &#039;solution&#039; ) ) {
        return;
    }

    add_submenu_page(
        &#039;edit.php?post_type=solution&#039;,
        &#039;內容關鍵字搜尋&#039;,
        &#039;內容關鍵字搜尋&#039;,
        &#039;edit_posts&#039;,
        &#039;solution-content-search&#039;,
        &#039;solution_content_search_page_render&#039;
    );
} );</code></pre>
<h2 class="wp-block-heading">搜尋頁面渲染與表單處理</h2>
<p><code>solution_content_search_page_render</code> 函式負責渲染搜尋表單與結果頁面。首先檢查使用者權限，避免未授權存取。接著判斷是否有表單送出，並使用 WordPress 的 nonce 機制驗證安全性。</p>
<p>關鍵字透過 <code>sanitize_text_field</code> 與 <code>wp_unslash</code> 清理，避免 XSS 與注入風險。</p>
<pre><code class="lang-php language-php php">if ( isset( $_POST[&#039;solution_content_search_submit&#039;] ) ) {
    check_admin_referer( &#039;solution_content_search_action&#039;, &#039;solution_content_search_nonce&#039; );

    $keyword  = isset( $_POST[&#039;solution_keyword&#039;] ) ? sanitize_text_field( wp_unslash( $_POST[&#039;solution_keyword&#039;] ) ) : &#039;&#039;;
    $searched = true;

    if ( $keyword !== &#039;&#039; ) {
        // ...
    }
}</code></pre>
<h2 class="wp-block-heading">透過 WP_Query 及字串比對精確搜尋</h2>
<p>WordPress 內建搜尋（<code>s</code> 參數）會搜尋標題、內容等欄位，但可能不夠精準。此處先用 <code>WP_Query</code> 以關鍵字搜尋取得可能相關文章 ID，然後逐篇讀取內容，使用 <code>mb_stripos</code>（若有）或 <code>stripos</code> 做不區分大小寫的字串比對，確保內容確實包含關鍵字。</p>
<pre><code class="lang-php language-php php">$query = new WP_Query( array(
    &#039;post_type&#039;      =&gt; &#039;solution&#039;,
    &#039;post_status&#039;    =&gt; &#039;any&#039;,
    &#039;posts_per_page&#039; =&gt; -1,
    &#039;s&#039;              =&gt; $keyword,
    &#039;fields&#039;         =&gt; &#039;ids&#039;,
) );

foreach ( $query-&gt;posts as $post_id ) {
    $post = get_post( $post_id );
    if ( ! $post ) {
        continue;
    }

    $content = wp_strip_all_tags( $post-&gt;post_content );

    if ( function_exists( &#039;mb_stripos&#039; ) ) {
        $pos = mb_stripos( $content, $keyword );
    } else {
        $pos = stripos( $content, $keyword );
    }

    if ( $pos !== false ) {
        $results[] = $post_id;
    }
}
wp_reset_postdata();</code></pre>
<h2 class="wp-block-heading">搜尋結果呈現與操作介面</h2>
<p>搜尋結果會以表格形式呈現，包含文章 ID、標題（連結至編輯頁面）、前台連結與編輯按鈕，方便快速查看與編輯。若無輸入關鍵字或找不到結果，會顯示相應提示訊息。</p>
<p>表單使用 WordPress 內建的 <code>wp_nonce_field</code> 與 <code>submit_button</code>，確保安全與一致性。</p>
<h2 class="wp-block-heading">實務應用與優化方向</h2>
<p>此搜尋功能適合用於自訂文章類型管理，尤其是內容量大且需要精準定位文本的場景。未來可考慮加入分頁功能避免一次載入過多文章，或結合全文索引外掛提升搜尋效能。也可擴充搜尋範圍至自訂欄位或分類。</p>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
// 在「solution」這個 post type 底下新增一個後台子選單：內容關鍵字搜尋
add_action( &#039;admin_menu&#039;, function () {
    // 確保有這個 post type
    if ( ! post_type_exists( &#039;solution&#039; ) ) {
        return;
    }

    add_submenu_page(
        &#039;edit.php?post_type=solution&#039;,          // 父層 (solution 列表底下)
        &#039;內容關鍵字搜尋&#039;,                        // 頁面標題
        &#039;內容關鍵字搜尋&#039;,                        // 左側選單名稱
        &#039;edit_posts&#039;,                           // 權限
        &#039;solution-content-search&#039;,              // slug
        &#039;solution_content_search_page_render&#039;   // callback
    );
} );

// 後台頁面：渲染搜尋表單與結果
function solution_content_search_page_render() {
    if ( ! current_user_can( &#039;edit_posts&#039; ) ) {
        wp_die( &#039;沒有權限檢視此頁面。&#039; );
    }

    $keyword  = &#039;&#039;;
    $results  = array();
    $searched = false;

    // 處理表單送出
    if ( isset( $_POST[&#039;solution_content_search_submit&#039;] ) ) {
        check_admin_referer( &#039;solution_content_search_action&#039;, &#039;solution_content_search_nonce&#039; );

        $keyword  = isset( $_POST[&#039;solution_keyword&#039;] ) ? sanitize_text_field( wp_unslash( $_POST[&#039;solution_keyword&#039;] ) ) : &#039;&#039;;
        $searched = true;

        if ( $keyword !== &#039;&#039; ) {
            // 先用 WP_Query 找出可能包含關鍵字的文章 (全文搜尋)
            $query = new WP_Query( array(
                &#039;post_type&#039;      =&gt; &#039;solution&#039;,
                &#039;post_status&#039;    =&gt; &#039;any&#039;,
                &#039;posts_per_page&#039; =&gt; -1,
                &#039;s&#039;              =&gt; $keyword,
                &#039;fields&#039;         =&gt; &#039;ids&#039;,
            ) );

            if ( $query-&gt;have_posts() ) {
                foreach ( $query-&gt;posts as $post_id ) {
                    $post = get_post( $post_id );
                    if ( ! $post ) {
                        continue;
                    }

                    // 從內容中做字串檢查（確保 content 真的有包含關鍵字）
                    $content = wp_strip_all_tags( $post-&gt;post_content );

                    if ( function_exists( &#039;mb_stripos&#039; ) ) {
                        $pos = mb_stripos( $content, $keyword );
                    } else {
                        $pos = stripos( $content, $keyword );
                    }

                    if ( $pos !== false ) {
                        $results[] = $post_id;
                    }
                }
            }
            wp_reset_postdata();
        }
    }

    ?&gt;
    &lt;div class=&quot;wrap&quot;&gt;

&lt;h1&gt;Solution 內容關鍵字搜尋&lt;/h1&gt;

&lt;p&gt;輸入一個關鍵字，系統會掃描 &amp;lt;code&amp;gt;solution&amp;lt;/code&amp;gt;
 文章類型的內容（content），只要有包含該字串，就列出標題與編輯連結。&lt;/p&gt;

        &lt;form method=&quot;post&quot; style=&quot;margin-top: 1em; margin-bottom: 2em;&quot;&gt;
            &lt;?php wp_nonce_field( &#039;solution_content_search_action&#039;, &#039;solution_content_search_nonce&#039; ); ?&gt;

            &lt;table class=&quot;form-table&quot; role=&quot;presentation&quot;&gt;

&lt;tr&gt;
                    &lt;th scope=&quot;row&quot;&gt;
                        &lt;label for=&quot;solution_keyword&quot;&gt;關鍵字字串&lt;/label&gt;
                    &lt;/th&gt;

&lt;td&gt;
                        &lt;input
                            type=&quot;text&quot;
                            id=&quot;solution_keyword&quot;
                            name=&quot;solution_keyword&quot;
                            class=&quot;regular-text&quot;
                            value=&quot;&lt;?php echo esc_attr( $keyword ); ?&gt;&quot;
                            placeholder=&quot;例如：API、某段程式碼、特定中文句子&quot;
                        /&gt;
                    &lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;

            &lt;?php submit_button( &#039;開始搜尋&#039;, &#039;primary&#039;, &#039;solution_content_search_submit&#039; ); ?&gt;
        &lt;/form&gt;

        &lt;?php if ( $searched ) : ?&gt;

&lt;h2&gt;搜尋結果&lt;/h2&gt;

            &lt;?php if ( $keyword === &#039;&#039; ) : ?&gt;
                &lt;div class=&quot;notice notice-warning&quot;&gt;&lt;p&gt;請輸入關鍵字。&lt;/p&gt;&lt;/div&gt;
            &lt;?php elseif ( empty( $results ) ) : ?&gt;
                &lt;div class=&quot;notice notice-info&quot;&gt;&lt;p&gt;沒有找到內容包含「&lt;?php echo esc_html( $keyword ); ?&gt;」的 solution 文章。&lt;/p&gt;&lt;/div&gt;
            &lt;?php else : ?&gt;

&lt;p&gt;找到 &lt;?php echo esc_html( count( $results ) ); ?&gt; 篇文章，內容中包含「&lt;?php echo esc_html( $keyword ); ?&gt;」。&lt;/p&gt;

                &lt;table class=&quot;widefat fixed striped&quot;&gt;

&lt;thead&gt;

&lt;tr&gt;
                            &lt;th scope=&quot;col&quot; style=&quot;width: 50px;&quot;&gt;ID&lt;/th&gt;
                            &lt;th scope=&quot;col&quot;&gt;標題&lt;/th&gt;
                            &lt;th scope=&quot;col&quot; style=&quot;width: 200px;&quot;&gt;前台連結&lt;/th&gt;
                            &lt;th scope=&quot;col&quot; style=&quot;width: 150px;&quot;&gt;編輯&lt;/th&gt;
                        &lt;/tr&gt;
                    &lt;/thead&gt;

&lt;tbody&gt;
                    &lt;?php foreach ( $results as $post_id ) :
                        $title     = get_the_title( $post_id );
                        $view_link = get_permalink( $post_id );
                        $edit_link = get_edit_post_link( $post_id, &#039;&#039; );
                    ?&gt;

&lt;tr&gt;

&lt;td&gt;&lt;?php echo esc_html( $post_id ); ?&gt;&lt;/td&gt;

&lt;td&gt;
                                &lt;?php if ( $edit_link ) : ?&gt;
                                    &lt;a href=&quot;&lt;?php echo esc_url( $edit_link ); ?&gt;&quot;&gt;
                                        &lt;?php echo esc_html( $title ); ?&gt;
                                    &lt;/a&gt;
                                &lt;?php else : ?&gt;
                                    &lt;?php echo esc_html( $title ); ?&gt;
                                &lt;?php endif; ?&gt;
                            &lt;/td&gt;

&lt;td&gt;
                                &lt;?php if ( $view_link ) : ?&gt;
                                    &lt;a href=&quot;&lt;?php echo esc_url( $view_link ); ?&gt;&quot; target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;
                                        檢視
                                    &lt;/a&gt;
                                &lt;?php else : ?&gt;
                                    -
                                &lt;?php endif; ?&gt;
                            &lt;/td&gt;

&lt;td&gt;
                                &lt;?php if ( $edit_link ) : ?&gt;
                                    &lt;a href=&quot;&lt;?php echo esc_url( $edit_link ); ?&gt;&quot; class=&quot;button button-small&quot;&gt;
                                        編輯文章
                                    &lt;/a&gt;
                                &lt;?php else : ?&gt;
                                    -
                                &lt;?php endif; ?&gt;
                            &lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;?php endforeach; ?&gt;
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;?php endif; ?&gt;
        &lt;?php endif; ?&gt;
    &lt;/div&gt;
    &lt;?php
}</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-solution-content-search/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress 自訂文章類型批次內容關鍵字取代工具實作</title>
		<link>https://piglife.tw/technical-notes/wordpress-solution-content-replace/</link>
					<comments>https://piglife.tw/technical-notes/wordpress-solution-content-replace/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Sun, 21 Dec 2025 22:20:56 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[後台功能]]></category>
		<category><![CDATA[批次替換]]></category>
		<category><![CDATA[自訂文章類型]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/wordpress-solution-content-replace/</guid>

					<description><![CDATA[介紹如何在 WordPress 自訂文章類型 solution 中新增後台子選單，實作批次搜尋並替換...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>在 WordPress 後台管理自訂文章類型（Custom Post Type）時，有時需要快速批次更新文章內容中的特定字串。這段程式碼示範如何在名為 <code>solution</code> 的自訂文章類型新增一個子選單，提供後台管理者輸入舊字串與新字串，並批次替換所有相關文章內容中的文字。適合有基本 WordPress 開發經驗，想要自訂後台功能以提升內容維護效率的工程師或自學者。</p>
<h2 class="wp-block-heading">新增子選單介面</h2>
<p>利用 <code>admin_menu</code> action 新增子選單，並限定該選單只在 <code>solution</code> 文章類型下顯示。權限設定為 <code>edit_posts</code>，確保只有有編輯文章權限的使用者能操作。</p>
<pre><code class="lang-php language-php php">add_action(&#039;admin_menu&#039;, function () {
    if (!post_type_exists(&#039;solution&#039;)) {
        return;
    }

    add_submenu_page(
        &#039;edit.php?post_type=solution&#039;,
        &#039;內容關鍵字取代&#039;,
        &#039;內容關鍵字取代&#039;,
        &#039;edit_posts&#039;,
        &#039;solution-content-replace&#039;,
        &#039;solution_content_replace_page&#039;
    );
});</code></pre>
<p>這段程式碼確保子選單只會在 <code>solution</code> 文章類型的管理頁面出現，並且點擊後會呼叫 <code>solution_content_replace_page</code> 函式來渲染頁面。</p>
<h2 class="wp-block-heading">後台頁面與表單處理</h2>
<p><code>solution_content_replace_page</code> 函式負責顯示輸入表單與處理替換邏輯。首先檢查使用者權限，避免未授權存取。接著使用 WordPress 的 Nonce 機制保障表單安全。</p>
<pre><code class="lang-php language-php php">if (!current_user_can(&#039;edit_posts&#039;)) {
    wp_die(&#039;沒有權限。&#039;);
}

if (isset($_POST[&#039;solution_replace_submit&#039;])) {
    check_admin_referer(&#039;solution_replace_action&#039;, &#039;solution_replace_nonce&#039;);

    $old = isset($_POST[&#039;old_keyword&#039;]) ? sanitize_text_field($_POST[&#039;old_keyword&#039;]) : &#039;&#039;;
    $new = isset($_POST[&#039;new_keyword&#039;]) ? sanitize_text_field($_POST[&#039;new_keyword&#039;]) : &#039;&#039;;

    // 進行批次替換
}</code></pre>
<p>使用者輸入的舊字串與新字串會經過 <code>sanitize_text_field</code> 清理，避免 XSS 或其他安全問題。</p>
<h2 class="wp-block-heading">批次搜尋與替換邏輯</h2>
<p>當舊字串不為空時，使用 <code>WP_Query</code> 撈取所有 <code>solution</code> 文章的 ID，並逐篇讀取內容。若文章內容包含舊字串，則使用 <code>str_replace</code> 替換後，呼叫 <code>wp_update_post</code> 更新文章。</p>
<pre><code class="lang-php language-php php">$query = new WP_Query([
    &#039;post_type&#039;      =&gt; &#039;solution&#039;,
    &#039;post_status&#039;    =&gt; &#039;any&#039;,
    &#039;posts_per_page&#039; =&gt; -1,
    &#039;fields&#039;         =&gt; &#039;ids&#039;,
]);

foreach ($query-&gt;posts as $post_id) {
    $post = get_post($post_id);
    if (!$post) {
        continue;
    }

    $content = $post-&gt;post_content;

    if (mb_strpos($content, $old) !== false) {
        $updated_content = str_replace($old, $new, $content);

        wp_update_post([
            &#039;ID&#039;           =&gt; $post_id,
            &#039;post_content&#039; =&gt; $updated_content
        ]);

        $results[] = [
            &#039;id&#039;     =&gt; $post_id,
            &#039;title&#039;  =&gt; $post-&gt;post_title,
            &#039;status&#039; =&gt; &#039;已替換&#039;
        ];
    }
}
wp_reset_postdata();</code></pre>
<h2 class="wp-block-heading">結果呈現與使用者體驗</h2>
<p>頁面會根據執行狀態顯示不同訊息：</p>
<ul>
<li>若未輸入舊字串，提醒使用者必須填寫。</li>
<li>若找不到包含舊字串的文章，顯示資訊提示。</li>
<li>若有替換成功的文章，列出文章 ID、標題、狀態與編輯連結，方便管理者後續檢視。</li>
</ul>
<p>這樣的設計讓使用者能清楚知道批次操作的結果，並快速跳轉編輯。</p>
<h2 class="wp-block-heading">實務應用與優化建議</h2>
<p>此功能適合內容量大且需定期文字修正的網站，如產品說明、技術文件等。未來可擴充：</p>
<ul>
<li>支援正則表達式替換，提高靈活度。</li>
<li>加入替換前後內容差異預覽，降低誤替換風險。</li>
<li>加入分頁或批次處理，避免大量文章一次更新造成伺服器負擔。</li>
</ul>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>請確認使用者權限設定正確，避免誤用。</li>
<li>替換操作無法還原，建議先備份資料庫。</li>
<li>文字替換為純字串，不支援 HTML 或多語系複雜處理。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
// 在 solution post type 底下新增子選單：內容關鍵字取代
add_action(&#039;admin_menu&#039;, function () {
    if (!post_type_exists(&#039;solution&#039;)) {
        return;
    }

    add_submenu_page(
        &#039;edit.php?post_type=solution&#039;,
        &#039;內容關鍵字取代&#039;,
        &#039;內容關鍵字取代&#039;,
        &#039;edit_posts&#039;,
        &#039;solution-content-replace&#039;,
        &#039;solution_content_replace_page&#039;
    );
});

// 後台頁面
function solution_content_replace_page() {
    if (!current_user_can(&#039;edit_posts&#039;)) {
        wp_die(&#039;沒有權限。&#039;);
    }

    $old = &#039;&#039;;
    $new = &#039;&#039;;
    $results = [];
    $executed = false;

    if (isset($_POST[&#039;solution_replace_submit&#039;])) {
        check_admin_referer(&#039;solution_replace_action&#039;, &#039;solution_replace_nonce&#039;);

        $old = isset($_POST[&#039;old_keyword&#039;]) ? sanitize_text_field($_POST[&#039;old_keyword&#039;]) : &#039;&#039;;
        $new = isset($_POST[&#039;new_keyword&#039;]) ? sanitize_text_field($_POST[&#039;new_keyword&#039;]) : &#039;&#039;;

        $executed = true;

        if ($old !== &#039;&#039;) {
            $query = new WP_Query([
                &#039;post_type&#039;      =&gt; &#039;solution&#039;,
                &#039;post_status&#039;    =&gt; &#039;any&#039;,
                &#039;posts_per_page&#039; =&gt; -1,
                &#039;fields&#039;         =&gt; &#039;ids&#039;,
            ]);

            foreach ($query-&gt;posts as $post_id) {
                $post = get_post($post_id);
                if (!$post) {
                    continue;
                }

                $content = $post-&gt;post_content;

                // 檢查舊字串是否存在
                if (mb_strpos($content, $old) !== false) {
                    // 替換
                    $updated_content = str_replace($old, $new, $content);

                    // 更新文章
                    wp_update_post([
                        &#039;ID&#039;           =&gt; $post_id,
                        &#039;post_content&#039; =&gt; $updated_content
                    ]);

                    $results[] = [
                        &#039;id&#039;     =&gt; $post_id,
                        &#039;title&#039;  =&gt; $post-&gt;post_title,
                        &#039;status&#039; =&gt; &#039;已替換&#039;
                    ];
                }
            }
            wp_reset_postdata();
        }
    }
    ?&gt;

&lt;div class=&quot;wrap&quot;&gt;

&lt;h2&gt;Solution &ndash; 內容關鍵字取代&lt;/h2&gt;

        &lt;form method=&quot;post&quot;&gt;
            &lt;?php wp_nonce_field(&#039;solution_replace_action&#039;, &#039;solution_replace_nonce&#039;); ?&gt;

            &lt;table class=&quot;form-table&quot;&gt;

&lt;tr&gt;

&lt;th&gt;&lt;label for=&quot;old_keyword&quot;&gt;要搜尋的字（舊字串）&lt;/label&gt;&lt;/th&gt;

&lt;td&gt;
                        &lt;input type=&quot;text&quot; id=&quot;old_keyword&quot; name=&quot;old_keyword&quot; class=&quot;regular-text&quot;
                               value=&quot;&lt;?php echo esc_attr($old); ?&gt;&quot;
                               placeholder=&quot;例如：舊字串&quot;&gt;
                    &lt;/td&gt;
                &lt;/tr&gt;

&lt;tr&gt;

&lt;th&gt;&lt;label for=&quot;new_keyword&quot;&gt;替換成（新字串）&lt;/label&gt;&lt;/th&gt;

&lt;td&gt;
                        &lt;input type=&quot;text&quot; id=&quot;new_keyword&quot; name=&quot;new_keyword&quot; class=&quot;regular-text&quot;
                               value=&quot;&lt;?php echo esc_attr($new); ?&gt;&quot;
                               placeholder=&quot;例如：新字串（可空白）&quot;&gt;
                    &lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;

            &lt;?php submit_button(&#039;開始批次取代&#039;, &#039;primary&#039;, &#039;solution_replace_submit&#039;); ?&gt;
        &lt;/form&gt;

        &lt;?php if ($executed): ?&gt;

&lt;h2&gt;取代結果&lt;/h2&gt;

            &lt;?php if ($old === &#039;&#039;): ?&gt;
                &lt;div class=&quot;notice notice-warning&quot;&gt;&lt;p&gt;請輸入要搜尋的舊字串。&lt;/p&gt;&lt;/div&gt;

            &lt;?php elseif (empty($results)): ?&gt;
                &lt;div class=&quot;notice notice-info&quot;&gt;&lt;p&gt;沒有任何文章包含「&lt;?php echo esc_html($old); ?&gt;」。&lt;/p&gt;&lt;/div&gt;

            &lt;?php else: ?&gt;
                &lt;table class=&quot;widefat striped&quot;&gt;

&lt;thead&gt;

&lt;tr&gt;

&lt;th&gt;ID&lt;/th&gt;

&lt;th&gt;標題&lt;/th&gt;

&lt;th&gt;狀態&lt;/th&gt;

&lt;th&gt;編輯連結&lt;/th&gt;
                    &lt;/tr&gt;
                    &lt;/thead&gt;

&lt;tbody&gt;
                    &lt;?php foreach ($results as $r): ?&gt;

&lt;tr&gt;

&lt;td&gt;&lt;?php echo esc_html($r[&#039;id&#039;]); ?&gt;&lt;/td&gt;

&lt;td&gt;&lt;?php echo esc_html($r[&#039;title&#039;]); ?&gt;&lt;/td&gt;

&lt;td&gt;&lt;?php echo esc_html($r[&#039;status&#039;]); ?&gt;&lt;/td&gt;

&lt;td&gt;&lt;a href=&quot;&lt;?php echo get_edit_post_link($r[&#039;id&#039;]); ?&gt;&quot; class=&quot;button button-small&quot;&gt;編輯&lt;/a&gt;&lt;/td&gt;
                        &lt;/tr&gt;
                    &lt;?php endforeach; ?&gt;
                    &lt;/tbody&gt;
                &lt;/table&gt;
            &lt;?php endif; ?&gt;

        &lt;?php endif; ?&gt;
    &lt;/div&gt;

    &lt;?php
}</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/wordpress-solution-content-replace/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>使用 Polylang 官方流程批量複製 WordPress 文章翻譯</title>
		<link>https://piglife.tw/technical-notes/polylang-bulk-copy-translations/</link>
					<comments>https://piglife.tw/technical-notes/polylang-bulk-copy-translations/#respond</comments>
		
		<dc:creator><![CDATA[小豬]]></dc:creator>
		<pubDate>Sat, 20 Dec 2025 22:21:10 +0000</pubDate>
				<category><![CDATA[技術筆記]]></category>
		<category><![CDATA[acf]]></category>
		<category><![CDATA[Polylang]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_insert_post]]></category>
		<category><![CDATA[多語系]]></category>
		<category><![CDATA[翻譯複製]]></category>
		<category><![CDATA[自訂文章類型]]></category>
		<guid isPermaLink="false">https://piglife.tw/technical-notes/polylang-bulk-copy-translations/</guid>

					<description><![CDATA[介紹如何使用 Polylang 官方 API 批量複製 WordPress 自訂文章類型的內容，建立...]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">前言</h2>
<p>這段 PHP 程式碼是為了解決 WordPress 使用 Polylang 多語系外掛時，如何批量將特定文章類型的內容從一種語言複製到另一種語言，並建立正確的翻譯關聯。適合需要管理大量多語系內容的網站管理員或開發者，尤其是想自動化翻譯文章複製流程的技術人員。</p>
<h2 class="wp-block-heading">Polylang 官方 API 的使用背景</h2>
<p>Polylang 提供了多個官方函式來管理文章語言與翻譯關聯，如 <code>pll_set_post_language</code> 設定文章語言，<code>pll_save_post_translations</code> 儲存翻譯群組，這是官方推薦的做法，能確保多語系資料完整且一致。</p>
<h2 class="wp-block-heading">後台介面設計與操作流程</h2>
<h3 class="wp-block-heading">後台子選單與頁面</h3>
<p>程式碼透過 <code>add_submenu_page</code> 在自訂文章類型「investor_relations」的管理頁面下新增子選單，提供單筆測試與批量複製兩種操作介面。使用者可選擇目標語言（目前限制為英文與日文），並執行對應動作。</p>
<h3 class="wp-block-heading">權限與安全檢查</h3>
<p>頁面載入時會檢查使用者是否有 <code>manage_options</code> 權限，並確認 Polylang API 函式存在，避免外掛未啟用或權限不足導致錯誤。</p>
<h2 class="wp-block-heading">核心功能：複製文章並建立翻譯關聯</h2>
<h3 class="wp-block-heading">1. 確認來源文章及語言設定</h3>
<p>函式 <code>ir_clone_post_official</code> 會先取得來源文章，並用 <code>pll_set_post_language</code> 強制設定來源文章語言，確保語言標記正確。</p>
<pre><code class="lang-php language-php php">ir_set_lang_official( $source_id, $source_lang );</code></pre>
<h3 class="wp-block-heading">2. 檢查目標語言翻譯是否已存在</h3>
<p>利用 <code>pll_get_post_translations</code> 取得該文章的所有語言版本，若目標語言已存在翻譯文章，則跳過建立，避免重複。</p>
<h3 class="wp-block-heading">3. 建立新文章（翻譯版本）</h3>
<p>使用 WordPress 官方函式 <code>wp_insert_post</code> 複製文章內容與屬性，並設定新文章的語言為目標語言。</p>
<pre><code class="lang-php language-php php">$new_id = wp_insert_post([...]);
ir_set_lang_official( $new_id, $target_lang );</code></pre>
<h3 class="wp-block-heading">4. 儲存翻譯關聯</h3>
<p>將來源文章與新文章的語言關聯資料更新至 Polylang，確保前台語言切換功能正常。</p>
<pre><code class="lang-php language-php php">$translations[ $target_lang ] = $new_id;
pll_save_post_translations( $translations );</code></pre>
<h3 class="wp-block-heading">5. 複製分類法與特色圖片</h3>
<p>排除語言分類法後，複製其他自訂分類法的關聯，並複製特色圖片，保持內容一致性。</p>
<h3 class="wp-block-heading">6. 複製 ACF 自訂欄位資料</h3>
<p>若有安裝 Advanced Custom Fields 外掛，會將所有欄位資料一併複製，包含上傳檔案，避免資料遺失。</p>
<h2 class="wp-block-heading">實務應用與優化建議</h2>
<ul>
<li><strong>備份資料庫</strong>：執行批量複製前務必備份，避免誤操作導致資料錯亂。</li>
<li><strong>單筆測試</strong>：先用單筆測試功能確保流程正常，再進行批量操作。</li>
<li><strong>擴充語言</strong>：可依需求新增允許的目標語言陣列。</li>
<li><strong>錯誤處理</strong>：可加強錯誤回報與日誌紀錄，方便除錯與維護。</li>
</ul>
<h2 class="wp-block-heading">常見問題與注意事項</h2>
<ul>
<li>Polylang API 函式不存在時，需確認外掛是否啟用。</li>
<li>複製文章時，若有其他外掛影響文章資料，可能需要額外處理。</li>
<li>語言分類法不應被複製，以免覆寫 Polylang 的語言設定。</li>
</ul>
<h2 class="wp-block-heading">完整程式碼</h2>
<pre><code class="lang-php language-php php">&lt;?php
/**
 * Investor Relations 單語言批量翻譯複製工具（最終版）
 * - 完全走 Polylang 官方流程：
 *   wp_insert_post + pll_set_post_language + pll_save_post_translations
 * - 自動跳過「已經有目標語言翻譯」的文章
 */

define( &#039;IR_SOURCE_LANG&#039;, &#039;zh&#039; );                  // 來源語言：中文
define( &#039;IR_POST_TYPE&#039;, &#039;investor_relations&#039; );    // Post type：investor_relations

/* =========================
 * 共用：設定語言（官方 API）
 * ========================= */
function ir_set_lang_official( $post_id, $lang_code ) {
    if ( function_exists( &#039;pll_set_post_language&#039; ) ) {
        pll_set_post_language( $post_id, $lang_code );
    }
}

/* =========================
 * 後台子選單
 * ========================= */
add_action( &#039;admin_menu&#039;, function () {
    add_submenu_page(
        &#039;edit.php?post_type=&#039; . IR_POST_TYPE,
        &#039;Bulk Copy Translations (Official)&#039;,
        &#039;Bulk Copy Translations (Official)&#039;,
        &#039;manage_options&#039;,
        &#039;ir-bulk-translations-official&#039;,
        &#039;ir_bulk_translations_official_page&#039;
    );
});

/* =========================
 * 後台頁面 + 控制流程
 * ========================= */
function ir_bulk_translations_official_page() {

    // 允許的目標語言（依照 Polylang 的 code）
    $allowed_target_langs = [ &#039;en&#039;, &#039;ja&#039; ];

    if ( ! current_user_can( &#039;manage_options&#039; ) ) {
        wp_die( &#039;沒有權限。&#039; );
    }

    if ( ! function_exists( &#039;pll_set_post_language&#039; ) || ! function_exists( &#039;pll_save_post_translations&#039; ) ) {
        echo &#039;&lt;div class=&quot;notice notice-error&quot;&gt;&lt;p&gt;Polylang API 不存在，請確認外掛是否啟用。&lt;/p&gt;&lt;/div&gt;&#039;;
        return;
    }

    // 目前選擇的目標語言（預設 en）
    $current_target = isset( $_POST[&#039;ir_target_lang&#039;] )
        ? sanitize_text_field( $_POST[&#039;ir_target_lang&#039;] )
        : &#039;en&#039;;

    if ( ! in_array( $current_target, $allowed_target_langs, true ) ) {
        $current_target = &#039;en&#039;;
    }

    echo &#039;
&lt;h1&gt;Investor Relations 翻譯複製工具（官方流程版）&lt;/h1&gt;&#039;;
    echo &#039;
&lt;p&gt;來源語言固定為：&lt;code&gt;&#039; . esc_html( IR_SOURCE_LANG ) . &#039;</code></p>';
    echo '<p style="color:red"><strong>⚠️ 執行前務必先備份資料庫！</strong></p>';

    echo '
.ir-lang-select{margin-left:8px;}';

    /* ------------ 單筆測試表單 ------------- */
    echo '
<p>單筆文章測試</p>';
    echo '';
    wp_nonce_field( 'ir_single_test_official' );
    echo '<label>輸入文章 ID：<input type="number" name="ir_test_id"></label>';

    echo '<span class="ir-lang-select">目標語言：';
    echo '';
    foreach ( $allowed_target_langs as $code ) {
        printf(
            '%s',
            esc_attr( $code ),
            selected( $current_target, $code, false ),
            esc_html( strtoupper( $code ) )
        );
    }
    echo '</span> ';

    submit_button( '測試複製此文章', 'secondary', 'ir_single_test', false );
    echo '';

    /* ------------ 批次表單 ------------- */
    echo '
<p>批量複製所有中文文章</p>';
    echo '';
    wp_nonce_field( 'ir_bulk_official' );

    echo '<span>目標語言：';
    echo '';
    foreach ( $allowed_target_langs as $code ) {
        printf(
            '%s',
            esc_attr( $code ),
            selected( $current_target, $code, false ),
            esc_html( strtoupper( $code ) )
        );
    }
    echo '</span> ';

    submit_button( '執行批次複製（請先單筆測試）', 'primary', 'ir_bulk_run', false );
    echo '';

    /* ------------ 執行動作 ------------- */

    // 單筆
    if ( isset( $_POST['ir_single_test'] ) ) {
        check_admin_referer( 'ir_single_test_official' );
        $post_id = isset( $_POST['ir_test_id'] ) ? intval( $_POST['ir_test_id'] ) : 0;
        $target  = isset( $_POST['ir_target_lang'] ) ? sanitize_text_field( $_POST['ir_target_lang'] ) : 'en';
        ir_run_single_copy_official( $post_id, $target );
    }

    // 批次
    if ( isset( $_POST['ir_bulk_run'] ) ) {
        check_admin_referer( 'ir_bulk_official' );
        $target = isset( $_POST['ir_target_lang'] ) ? sanitize_text_field( $_POST['ir_target_lang'] ) : 'en';
        ir_run_bulk_copy_official( $target );
    }

}</code></pre>
<pre><code class="lang-php language-php php">function ir_run_single_copy_official( $post_id, $target_lang ) {
    echo &#039;
&lt;h3&gt;單筆測試結果&lt;/h3&gt;&#039;;

    if ( ! $post_id || ! get_post( $post_id ) ) {
        echo &#039;&lt;p style=&quot;color:red;&quot;&gt;找不到文章 ID：&#039; . esc_html( $post_id ) . &#039;&lt;/p&gt;&#039;;
        return;
    }

    $report = ir_clone_post_official( $post_id, $target_lang );

    echo &#039;&lt;pre style=&quot;background:white; padding:10px; border:1px solid #ccc;&quot;&gt;&#039;;
    echo esc_html( implode( &quot;\n&quot;, $report ) );
    echo &#039;&lt;/pre&gt;&#039;;
}

/* =========================
 * 批量處理所有中文文章（官方流程）
 * ========================= */
function ir_run_bulk_copy_official( $target_lang ) {
    $posts = get_posts( [
        &#039;post_type&#039;      =&gt; IR_POST_TYPE,
        &#039;posts_per_page&#039; =&gt; -1,
        &#039;post_status&#039;    =&gt; &#039;any&#039;,
        &#039;lang&#039;           =&gt; IR_SOURCE_LANG, // Polylang 的語言 query var
        &#039;fields&#039;         =&gt; &#039;ids&#039;,
    ] );

    echo &#039;
&lt;h2&gt;批次執行結果（目標語言：&#039; . esc_html( strtoupper( $target_lang ) ) . &#039;）&lt;/h2&gt;&#039;;

    if ( empty( $posts ) ) {
        echo &#039;
&lt;p&gt;沒有找到來源語言（&#039; . esc_html( IR_SOURCE_LANG ) . &#039;）的文章。&lt;/p&gt;&#039;;
        return;
    }

    $output = [];

    foreach ( $posts as $post_id ) {
        $report  = ir_clone_post_official( $post_id, $target_lang );
        $output  = array_merge( $output, $report );
    }

    echo &#039;&lt;pre style=&quot;background:white; max-height:450px; overflow:auto; padding:10px; border:1px solid #ccc;&quot;&gt;&#039;;
    echo esc_html( implode( &quot;\n&quot;, $output ) );
    echo &#039;&lt;/pre&gt;&#039;;
}

/* =========================
 * 核心：官方流程複製一篇文章 &rarr; 目標語言翻譯
 * ========================= */
function ir_clone_post_official( $source_id, $target_lang ) {
    $report = [];

    $source_post = get_post( $source_id );
    if ( ! $source_post ) {
        $report[] = &quot;❌ 原文 {$source_id} 不存在&quot;;
        return $report;
    }

    $source_lang = IR_SOURCE_LANG;

    // Step 1) 先確保原文語言正確
    ir_set_lang_official( $source_id, $source_lang );
    $report[] = &quot;Step 1) 設定原文語言：{$source_id} &rarr; {$source_lang}&quot;;

    // 取得現有翻譯 group
    $translations = function_exists( &#039;pll_get_post_translations&#039; )
        ? pll_get_post_translations( $source_id )
        : [];

    if ( empty( $translations[ $source_lang ] ) ) {
        $translations[ $source_lang ] = $source_id;
    }

    // ✅ 若已存在該語言翻譯 &rarr; 自動跳過
    if ( ! empty( $translations[ $target_lang ] ) ) {
        $existing = $translations[ $target_lang ];
        $report[] = &quot;⚠️ 原文 {$source_id} 已有 {$target_lang} 翻譯（ID {$existing}），跳過建立新文章。&quot;;

        // 做個語言 debug
        $lang_debug = [];
        foreach ( $translations as $code =&gt; $pid ) {
            $lang_debug[] = $code . &#039;:&#039; . $pid . &#039;(&#039; . pll_get_post_language( $pid ) . &#039;)&#039;;
        }
        $report[] = &#039;🧪 目前語言狀態：&#039; . implode( &#039;, &#039;, $lang_debug );

        return $report;
    }

    // Step 2) 使用 wp_insert_post 建立新文章（官方建議做法）
    $new_id = wp_insert_post( [
        &#039;post_type&#039;      =&gt; $source_post-&gt;post_type,
        &#039;post_status&#039;    =&gt; $source_post-&gt;post_status,
        &#039;post_title&#039;     =&gt; $source_post-&gt;post_title,
        &#039;post_content&#039;   =&gt; $source_post-&gt;post_content,
        &#039;post_excerpt&#039;   =&gt; $source_post-&gt;post_excerpt,
        &#039;post_author&#039;    =&gt; $source_post-&gt;post_author,
        &#039;post_date&#039;      =&gt; $source_post-&gt;post_date,
        &#039;post_date_gmt&#039;  =&gt; $source_post-&gt;post_date_gmt,
        &#039;menu_order&#039;     =&gt; $source_post-&gt;menu_order,
    ], true );

    if ( is_wp_error( $new_id ) ) {
        $report[] = &quot;❌ wp_insert_post 錯誤：&quot; . $new_id-&gt;get_error_message();
        return $report;
    }
    $report[] = &quot;Step 2) 建立新文章：{$new_id}&quot;;

    // Step 3) 官方建議：先設定新文章語言
    ir_set_lang_official( $new_id, $target_lang );
    $report[] = &quot;Step 3) 設定新文章語言：{$new_id} &rarr; {$target_lang}&quot;;

    // Step 4) 官方建議：建立翻譯關聯（保留既有語言）
    $translations[ $target_lang ] = $new_id;
    pll_save_post_translations( $translations );
    $report[] = &quot;Step 4) 儲存翻譯關聯：&quot; . json_encode( $translations );

    // Step 5) 複製 taxonomy（排除語言 taxonomy，避免覆寫語言）
    $taxes = get_object_taxonomies( IR_POST_TYPE );
    $taxes = array_diff( $taxes, [ &#039;language&#039;, &#039;pll_language&#039; ] );

    foreach ( $taxes as $tax ) {
        $terms = wp_get_object_terms( $source_id, $tax, [ &#039;fields&#039; =&gt; &#039;ids&#039; ] );
        if ( ! is_wp_error( $terms ) ) {
            wp_set_object_terms( $new_id, $terms, $tax, false );
        }
    }

    // Step 6) 複製特色圖片
    $thumb = get_post_thumbnail_id( $source_id );
    if ( $thumb ) {
        set_post_thumbnail( $new_id, $thumb );
    }

    // Step 7) 複製 ACF 欄位（包含上傳檔案）
    if ( function_exists( &#039;get_field_objects&#039; ) &amp;&amp; function_exists( &#039;update_field&#039; ) ) {
        $fields = get_field_objects( $source_id );
        if ( $fields ) {
            foreach ( $fields as $field ) {
                update_field( $field[&#039;key&#039;], $field[&#039;value&#039;], $new_id );
            }
        }
    }

    // 最後：實際語言檢查
    $src_lang_real = pll_get_post_language( $source_id );
    $new_lang_real = pll_get_post_language( $new_id );

    $report[] = &quot;🧪 實際語言檢查：原文 {$source_id} 語言：{$src_lang_real}；新文 {$new_id} 語言：{$new_lang_real}&quot;;
    $report[] = &quot;✅ 完成：原文 {$source_id} &rarr; {$target_lang} 翻譯 {$new_id}&quot;;

    return $report;
}</code></pre>]]></content:encoded>
					
					<wfw:commentRss>https://piglife.tw/technical-notes/polylang-bulk-copy-translations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
