從 WordPress 遷移

本頁內容

EmDash 提供從 WordPress 的完整遷移路徑。無需 CLI,在管理後台即可匯入文章、頁面、媒體與分類法。

開始之前

匯出內容

在 WordPress 中前往 工具 → 匯出,下載完整匯出檔(.xml)。

備份網站

確認遷移成功前,請讓 WordPress 網站保持上線。

匯入方式

EmDash 支援三種匯入 WordPress 內容的方式:

方式適用情境含草稿需認證
WXR 檔案上傳完整遷移
WordPress.com託管於 WordPress.com 的網站OAuth
REST API(探測)匯出前先檢視內容概要選用

多數遷移建議使用 WXR 檔上傳,可涵蓋草稿、自訂欄位與私密文章等全部內容。

WXR 檔案匯入

  1. 從 WordPress 匯出

    在 WordPress 後台:工具 → 匯出 → 所有內容 → 下載匯出檔

  2. 開啟匯入精靈

    在 EmDash:管理 → 設定 → 匯入 → WordPress

  3. 上傳匯出檔

    拖放 .xml 或點選瀏覽。檔案在瀏覽器中解析。

  4. 檢視偵測到的內容

    精靈會顯示類似:

    Found in export:
    ├── Posts: 127 → posts [New collection]
    ├── Pages: 12  → pages [Add fields]
    └── Media: 89 attachments
  5. 設定對應

    選擇要匯入的文章類型。EmDash 會:

    • 為未對應的文章類型自動建立集合(collection)
    • 為既有集合補上缺少的欄位
    • 對欄位型別衝突提出警告
  6. 執行匯入

    點選 匯入內容,可看到每個項目的處理進度。

  7. 匯入媒體(選用)

    內容匯入後,可選擇是否下載媒體檔。EmDash 會:

    • 從 WordPress URL 下載
    • 以內容雜湊去重
    • 自動改寫內容中的 URL

內容轉換

Gutenberg 到 Portable Text

EmDash 將 Gutenberg 區塊轉為 Portable Text 結構化格式。

Gutenberg 區塊Portable Text說明
core/paragraphblock style=“normal”保留行內標記
core/headingblock style=“h1-h6”層級來自區塊屬性
core/imageimage 區塊更新媒體參照
core/listlistItem 型別的 block有序與無序清單
core/quoteblock style=“blockquote”含引用資訊
core/codecode 區塊保留語言屬性
core/embedembed 區塊儲存 URL 與提供者
core/gallerygallery 區塊圖片參照陣列
core/columnscolumns 區塊保留巢狀內容
未知區塊htmlBlock保留原始 HTML 供複查

未知區塊以 htmlBlock 儲存,附原始 HTML 與區塊中繼資料。可手動處理或撰寫自訂 Portable Text 元件渲染。

傳統編輯器內容

傳統編輯器的 HTML 會轉成 Portable Text 區塊。行內樣式(<strong><em><a>)變成 span 上的標記。

狀態對應

WordPress 狀態EmDash 狀態
publishpublished
draftdraft
pendingpending
privateprivate
futurescheduled
trasharchived

分類法匯入

分類與標籤以分類法匯入,階層保留:

WordPress:                    EmDash:
├── Categories (hierarchical) ├── taxonomies table
│   ├── News                  │   ├── category/news
│   │   ├── Local             │   ├── category/local (parent: news)
│   │   └── World             │   ├── category/world (parent: news)
│   └── Sports                │   └── category/sports
└── Tags (flat)               └── content_taxonomies junction
    ├── featured                  ├── tag/featured
    └── breaking                  └── tag/breaking

自訂欄位與 ACF

文章詮釋資料與 ACF 欄位會在匯入時分析:

  1. 分析階段

    精靈會偵測自訂欄位並建議 EmDash 欄位型別:

    Custom Fields:
    ├── subtitle (string, 45 posts)
    ├── _yoast_wpseo_title → seo.title (string, 127 posts)
    ├── _thumbnail_id → featuredImage (reference, 89 posts)
    └── price (number, 23 posts)
  2. 欄位對應

    _edit__wp_ 開頭的內部欄位預設隱藏。SEO 外掛欄位對應到 seo 物件。

  3. 型別推斷

    EmDash 依值推斷型別:

    • 數字字串 → number
    • "1""0""true""false"boolean
    • ISO 日期 → date
    • 序列化的 PHP/JSON → json
    • WordPress ID(如 _thumbnail_id)→ reference

URL 重新導向

匯入後 EmDash 會產生重新導向對照:

{
	"redirects": [
		{ "from": "/?p=123", "to": "/posts/hello-world" },
		{ "from": "/2024/01/hello-world/", "to": "/posts/hello-world" },
		{ "from": "/category/news/", "to": "/categories/news" }
	],
	"feeds": [
		{ "from": "/feed/", "to": "/rss.xml" },
		{ "from": "/feed/atom/", "to": "/atom.xml" }
	]
}

可套用至:

  • Cloudflare 重新導向規則
  • 主機平台的重新導向設定
  • astro.config.mjs 中 Astro 的 redirects 選項

概念對照表

將 WordPress 慣例對應到 EmDash 時可參考:

WordPressEmDash說明
register_post_type()管理後台中的集合透過控制台或 API 建立
register_taxonomy()分類法或陣列欄位視複雜度而定
register_meta()集合 schema 中的欄位有型別,非任意鍵值
WP_QuerygetCollection(filters)執行階段查詢
get_post()getEntry(collection, id)回傳項目或 null
wp_insert_post()POST /_emdash/api/content/{type}REST API
the_content<PortableText value={...} />Portable Text 渲染
add_shortcode()Portable Text 自訂區塊自訂渲染元件
register_block_type()Portable Text 自訂區塊與短代碼類似
add_menu_page()外掛管理頁位於 /_emdash/admin/
add_action/filter()外掛勾點hooks.content:beforeSave
wp_optionsctx.kv鍵值儲存
wp_postmeta集合欄位結構化儲存
$wpdbctx.storage直接存取儲存層
分類/標籤分類法保留階層

API 匯入(進階)

WordPress 匯入可透過管理後台與 REST API 使用。為取得欄位對應、衝突處理與進度追蹤,建議使用匯入精靈。

程式化存取使用 /_emdash/api/import/wordpress/ 下的端點。

疑難排解

「XML parsing error」

匯出檔可能損毀或不完整,請從 WordPress 重新匯出。

媒體下載失敗

部分圖片可能需登入或 URL 已變更。匯入會繼續,失敗的 URL 會記錄。

欄位型別衝突

若既有集合中有不相容型別的欄位,精靈會提示衝突。您可以:

  • 重新命名 EmDash 中的欄位
  • 調整 WordPress 欄位對應
  • 刪除並重建該集合

超大匯出

超過約 100MB 時建議:

  1. 在 WordPress 依文章類型分別匯出
  2. 依序逐檔匯入
  3. 使用含 --resume 的 CLI 以提高可靠度

下一步