[WordPress] 外掛分享: No Slug Conflicts with Trash

WordPress 外掛 No Slug Conflicts with Trash 的封面圖片。

前言介紹

  • 這款 WordPress 外掛「No Slug Conflicts with Trash」是 2013-11-25 上架。
  • 目前有 10 個安裝啟用數。
  • 上一次更新是 2016-06-26,距離現在已有 3532 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 3.5 以上版本才可以安裝。
  • 有 2 人給過評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

coffee2code |

外掛標籤

post | slug | trash | post_name | coffee2code |

內容簡介

注意:WordPress 4.5 已將此外掛的功能整併到其中,因此除非您仍在運行舊版本的 WP,否則不再需要使用它。

此外掛的實作基於一個信念,即垃圾桶內的網頁或文章不應該在網址別名方面與新網頁或文章有任何衝突。基本上,新的網頁或文章應當優先於所有在垃圾桶內的內容。畢竟,這些網頁或文章是被放到垃圾桶內的,理應有其原因。

預設情況下,WordPress 在判斷新文章的別名是否已被使用時,會盤算之前已被移至垃圾桶內的文章和網頁。當然,在一般情況下,WordPress 不應該允許重複的別名,因為這可能會影響永久連結。WordPress 的處理方案的考量在於,被放到垃圾桶內的文章/網頁在技術上仍存在,但不能夠直接存取。作者或管理員可能希望將其從垃圾桶中恢復,那麼 WordPress 認為該文章/網頁應再次使用與被刪除之前相同的永久連結。

如果您仍不明白 WordPress 的做法,這裡舉個例子: WordPress 附帶了一個名為 "About" 的網頁,其別名為 "about"。該網頁的 URL 是 http://yoursite/about/。現在,假設您將這個網頁刪除,並建立了一個新的名為 "About" 的網頁。由於垃圾桶內有一個與新網頁相同的別名( "about"),新網頁因此被分配了 "about-2" 的別名,導致該網頁的 URL 成為 http://yoursite/about-2/。使用此外掛,新的 "About" 網頁將獲得希望的 "about" 別名。

另外,此外掛會嘗試將已還原的文章恢復到其原始的別名。唯一失敗的情況是,如果新的網頁或文章使用了原來在垃圾桶內的文章/網頁的別名,此外掛就無法完成恢復操作,而必須將該還原的文章/網頁分配一個新的別名。

有關此外掛功能更多深入的解說,請參閱文件中的其他章節。欲瞭解更多此問題的討論,請參閱 WP 核心 "ticket #11863"。

相關連結:此外掛首頁、此外掛目錄頁、作者首頁。

解決方案的詳細解釋

由此外掛採用的方法概述,以解決別名可能與垃圾桶內文章發生衝突的問題。

WordPress 原始行為

為了理解實現解決方案的核心,以下簡單說明 WP 的唯一別名處理方式:

在發布之前,所需的別名會傳遞到 wp_unique_post_slug() 並返回一個安全的別名。如果任何現有的文章(包括垃圾桶內的文章)使用該別名(或者不適合 URL 永久連結,如 RSS 資源或輸出彙整),則可能導致使用的安全別名與所需別名不同。

為避免垃圾桶內文章的別名與新文章發生衝突,此外掛採用了其中兩個主要任務:

1. 允許文章使用與垃圾桶內文章相同的別名

此外掛會 hook 上 ‘wp_unique_post_slug’ 过滤器。如果所需的別名與安全別名相同,則所需的別名是唯一的,不需要進行其他處理。

如果兩個別名不同,那就必須判斷衝突是否是由於垃圾桶內的文章引起的。因此,此外掛嘗試查找是否有一篇名為該別名的已經被放到垃圾桶內的文章。

如果沒有這樣的垃圾桶內文章,衝突就是出現在一篇目前進行中的文章上(或之前完全刪除的文章上,這種情況很少發生),因此這篇文章不能使用所需別名。
如果找到了名為該別名的垃圾桶內文章,則所需的別名是希望配置給新文章的。為此,此外掛從 DB 中刪除所找到的垃圾桶內文章(而不是將其完全刪除),以便新文章可使用希望的別名。

2. 確認所有還原的文章都使用原本的別名

此外掛會監聽 WordPress 中垃圾桶功能的行爲,在垃圾桶中的文章還原的時候,它會嘗試將這篇文章的別名還原為原始別名。唯一失敗的情況是,如果新的網頁或文章使用了在垃圾桶內的文章的原始別名,此外掛就無法還原該文章的別名,必須為其分配一個新別名。

如果還原的文章與現有文章衝突(即,現有文章使用了被還原文章原始的別名),則無法進行還原。因此,必須將該還原文章分配一個新的別名。

原文外掛簡介

NOTE: WordPress 4.5 incorporated the functionality of this plugin and thus it is no longer needed unless you’re still running an older version of WP.
This plugin implements the belief that a trashed page or post should not in any way conflict with a new page or post when it comes to slugs. In essence, a new page or post should take precedence over anything in the trash. After all, the page/post is in the trash for a reason.
By default, WordPress takes into consideration posts and pages that have been trashed when deciding if the slug for a new post is already in use. Obviously, in general, WordPress should not allow duplicate slugs because that could interfere with permalinks. The thinking behind WordPress’s handling of the situation is that trashed posts/pages are still technically present, just inaccessible. It is possible that an author or admin would choose to restore a post from the trash, which WordPress feels should then occupy that same permalink as before it was trashed.
If what WordPress does is unclear, here’s an example to help clarify things:

WordPress ships with a page called “About” with the slug of “about”. The page’s URL would be http://yoursite/about/
Let’s say you trash that page and start a new page with the name “About”.
Due to a trashed page having the slug that would normally have been assigned to the new page (“about”), the new page gets the slug of “about-2”, resulting in the page’s URL being http://yoursite/about-2/

With this plugin, for this example, the new “About” page would get the slug “about” as one would hope.
That said, the plugin tries its best to restore untrashed posts to their original slug. The only time it fails to do so is if a new page or post has claimed the trashed post’s original slug, in which case the untrashed post is assigned a new slug.
See other sections of the documentation for more insight into the plugin’s functionality. See WP core ticket #11863 for discussion on the matter.
Links: Plugin Homepage | Plugin Directory Page | Author Homepage
The Solution Explained
An overview of the approach employed by the plugin to resolve the issue of slugs potentially conflicting with posts in the trash.
What WordPress does
In order to understand the crux of the implemented solution, a quick refresher on unique slug handling by WP:
Before published use, a desired slug is passed to wp_unique_post_slug() and a safe slug is returned. The safe slug may differ from the desired slug if any existing post (including a trashed post) has that slug (or, less likely, is invalid for permalink reasons such as feeds or date archives).
In order to prevent a trashed post’s slug from conflicting with a new post, this plugin takes an approach that is comprised of two primary tasks:
1. Permit a post to use a slug that conflicts with a trashed post.
The plugin hooks the ‘wp_unique_post_slug’ filter. If the desired slug matches the safe slug, the desired slug is unique and nothing needs to be done.
When the two slugs don’t match, it has to determine if the conflict is due to a trashed post or not, so it attempts to find a trashed post with the desired slug.

If no such trashed post is found, the conflict is with a live post (or was an otherwise unsuitable slug) so the safe slug must be used by the new post (so, again, nothing needs to be done).
If such a trashed post is found, then the plugin simply inverses the traditional behavior of wp_unique_post_slug(): give the trashed post the safe slug and return the desired slug for use by the new post (rather than having the trashed post retain its slug and forcing the new post to use the safe slug). In order to be able to restore a trashed post to its original slug, the trashed post’s original slug is stored in postmeta.

2. Restore a trashed post to its original slug, if necessary and possible.
For a post transitioning away from the ‘trash’ post status, check to see if its slug was rewritten (denoted by the presence of the postmeta field only assigned if the original slug was changed due to a conflict while the post was in the trash).
If there was no original slug, the slug was never changed and nothing more needs to happen.
When there is an original slug, check to see if that slug is still in use by a non-trashed post.

If not, restore the post slug to the original value.
If so, there is no recourse but to leave the post slug as-is (it is guaranteed to be unique already).

In either case, the postmeta field for the original slug value gets deleted since the now untrashed post is exposed in some fashion with its current slug and should abide by it going forward.

各版本下載點

  • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
  • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「No Slug Conflicts with Trash」來進行安裝。

(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


1.0 | 1.1 | 1.2 | 1.0.1 | 1.0.2 | 1.0.3 | 1.0.4 | trunk |

延伸相關外掛(你可能也想知道)

  • Remove CPT base 》從網址中移除自訂文章類型基本標誌, , 可以選擇特定的自訂文章類型, 自動重新導向舊的基本標誌到無基礎標誌, 。
  • Remove Taxonomy Base Slug 》如果您想要對您的自訂文章類型文章使用更好的固定連結,並使用自訂的固定連結,這個外掛程序可以協助您。, 如果該術語的slug和文章類型相同,那麼該分類的優...。
  • Admin Slug Column 》這個外掛會在「所有文章」和「所有頁面」的 wp-admin 界面上新增一欄,顯示每一個文章或頁面的 URL 路徑和別名。如果某個頁面是某個父頁面的子頁面,也會顯示...。
  • Wenprise Pinyin Slug 》這個 WordPress 外掛可以自動將中文文章別名、分類項目別名和圖片文件名轉換為漢語拼音或英文翻譯。, 外掛的主要功能, , 將文章別名轉換為拼音或英文翻譯,支...。
  • WP Author Slug 》這個外掛會以使用者顯示名稱的已消毒版本來替換作者標誌。, 這個外掛無法防止駭客找到你的登錄名稱,但登錄名稱不會顯示在作者檔案的 URL 中,並且可以使用顯...。
  • Remove slug from custom post type 》在 WordPress 2.9 中引入了自定義文章類型,為許多 WordPress 開發人員打開了新世界。, 當涉及到移除 slug 並且不破壞其他文章類型的重寫規則時,這款外掛表...。
  • LONG URL MAKER 》將您的網址變得更長, 功能, , 使得您可以為文章、頁面、產品(Woocommerce)、分類和標籤設置更長的名稱而不會引起錯誤, 當有重複的名稱時,此插件可自動添加...。
  • SF Author Url Control 》使用此外掛,管理員可以更改已註冊使用者的預設作家基礎網址和每個使用者的作家標記。, 更改作家標記對於安全性來說是好事(如果您的登錄名稱是“ This Is Me”...。
  • Remove Taxonomy Url 》永久連結(Permalinks),如其名,是指指向網站頁面、文章、分類或標籤的永久連結,讓訪客和搜尋引擎更容易地瀏覽您的網站。, 您有多少次因為 URL 看起來古怪...。
  • Automatically Update Permalinks 》當啟用此外掛時,若一篇文章或頁面的標題更動了,permalink (slug) 將會自動更新以符合新標題。儲存時有一個選項可以暫時禁用此功能。, 如果您喜歡這個外掛,...。
  • German Slugs 》注意:如果網站語言為德文,最近版本的 WordPress 已經能夠正確轉換韻律短音。在這種情況下,就不需要安裝 German Slugs 了。, 在創建帖子或頁面時,WordPres...。
  • Slug or PostID 》此外掛使用Slug或PostID來作為文章名稱,並且此功能是自動進行的。, 其他內容, 如果您想要使用其他應用程式(例如「Windows Live Writer」)發布文章,您必須...。
  • Custom Post Type 》此插件可建立自訂文章類型。。
  • Remove Taxonomy Slug 》這個外掛可以讓您擁有乾淨的 SEO 友善網址。您可以透過幾個點擊來移除分類法(taxonomy)別名並取得 SEO 友善的網址。, 此外掛可以透過進入外掛設定來移除自...。
  • WP Slug Post Type Custom Language (Polylang) 》描述, 將自訂文章類型內部 URL (Slug) 更改為系統所需的語言。, 利用 Polylang 或 WordPress 系統中的想要的語言連結,將內部 URL (Slug) 更改為自訂文章類型...。

文章
Filter
Apply Filters
Mastodon