
有時,您會遇到一種情況,即您想要建立與網站中現有文章或頁面相同(或幾乎相同)的新文章或頁面。
如果文章或頁面很長或包含大量媒體,重新新增所有內容的過程似乎非常費力。此外,如果您有很多彼此非常相似的文章,那麼必須再次手動輸入所有文字或新增所有媒體檔案來建立新文章或頁面會非常煩人。
一種選擇可能是在一個瀏覽器視窗或選項卡中開啟現有文章(為方便起見,我們將使用“文章”來指代文章和頁面),然後在另一個視窗中建立新文章並在其中兩個,複製和貼上您的內容。畢竟,最近(古騰堡)版本的WordPress確實使這變得非常容易,您可以使用“複製所有內容”選項。
但是如果你必須一次又一次地這樣做,就有犯錯的風險。如果您不小心編輯了原始文章,覆蓋了您已釋出到網站的內容,該怎麼辦?或者,如果您要複製大量文章,而忘記建立了哪些文章?
這可能是一個潛在的頭痛。
幸運的是,有一種更簡單的方法。有可用的免費外掛可以讓您複製您的文章和頁面,因此您可以編輯和調整它們以建立與您現有內容幾乎相同的新文章。
如何在WordPress中複製頁面或文章:
您可以使用以下外掛之一在WordPress中輕鬆複製頁面、文章和自定義文章型別:
- Duplicate Posts: 它在 WordPress 管理中為每個文章/頁面新增一個“克隆”連結。
- Duplicate Page plugin: 它在每個文章、頁面或自定義文章型別下新增一個“複製此”連結。
- WP Bulk Post Duplicator:此外掛允許您批量複製文章/頁面。
如果您使用WooCommerce並需要複製您的產品,請將滑鼠懸停在任何產品上:您會在該產品下看到一個複製連結。
支援在這篇文章中,我們將向您展示一些可以用來執行此操作的方法,並重點介紹一些適合該任務的最佳外掛。
但首先,我們將向您展示如何在沒有外掛的情況下以半手動方式在WordPress中複製頁面或文章。這涉及複製和貼上,但WordPress使這件事變得如此容易,如果您只有一個文章要複製,它可能會比安裝外掛並這樣做更快。
如何在WordPress中複製頁面或文章(半自動)
如果您只有一個文章要複製,這種方法會更快,因為它可以節省您安裝外掛的時間。如果你有一堆文章要複製,我強烈建議你跳過這一節:你會發現使用外掛更快,更不用說更準確了。
所以對於那些只需要複製一篇文章或頁面的人來說,這裡是如何做到的。
首先開啟您現有的文章或頁面。通過單擊文章上的區塊外部或選擇螢幕右側編輯窗格中的“文件”選項卡,確保您沒有選擇單個區塊。如果您選擇了一個區塊,WordPress只會複製該區塊的內容,而不是整個文件。
現在單擊編輯螢幕右上角的省略號(三個點排列在一起)。

WordPress中的複製所有內容選項
將出現一個選單。在此選單的底部,您將看到“複製所有內容”選項。單擊它,您將收到一條訊息,告訴您您的內容已被複制。
現在建立您的新文章。為您的文章命名,然後單擊編輯螢幕上為您建立的第一個空白區塊。使用鍵盤上的快捷鍵進行貼上(PC 上為 Ctrl + V,Mac 上為 Cmd + V,或右鍵單擊並在出現的快捷選單中選擇貼上。
您舊文章的內容現在將出現在您的新文章中。
現在您可以編輯該文章並使其與舊文章不同。這是我強烈建議做的事情——在您的網站上釋出彼此完全重複的部落格文章並沒有多大意義。它不僅會使訪問者感到困惑,而且對SEO也不利。
但是,如果您想為文章建立多個副本怎麼辦?繼續閱讀……
如何使用外掛在WordPress中複製頁面或文章
到目前為止,複製文章最快、最簡單、最可靠的方法是使用外掛。這將節省您的時間,避免任何人為錯誤,並允許您複製您喜歡的任何文章型別。事實上,根據我們的經驗,複製文章實際上更常見於自定義文章型別。有兩篇幾乎完全相同的部落格文章是不尋常的,但例如,對於商店中的產品來說,這是很常見的。
所以這裡有一些外掛可以簡化複製文章的過程,以及如何使用它們的指南。
WooCommerce
您可能想知道為什麼我在這裡包含WooCommerce,因為它是一個電子商務外掛,而不是一個複製後外掛。
但值得一提的是,WooCommerce允許您在不安裝額外外掛的情況下複製產品。因此,在您開始搜尋後複製外掛並使用它來複制您的產品之前,只需使用WooCommerce捆綁的功能即可。
如果您轉到WooCommerce的主產品螢幕並將滑鼠懸停在任何產品上,您將在該文章下看到一個重複連結。

WooCommerce重複產品選項
只需單擊該重複連結,就會建立一個新產品。您將被直接帶到新產品的編輯螢幕,以便您可以對其進行更改然後釋出。
值得注意的是,新產品的slug將是舊產品的slug加上-copy,所以即使你改變了新產品的標題,你也需要編輯slug。

編輯重複的產品
Duplicate Post外掛
Duplicate Post是最受歡迎的文章複製外掛,下載量超過300萬次。
啟用它後,它會在WordPress管理員的主文章螢幕中為每個文章新增一個克隆連結。

Duplicate Post外掛
單擊您要複製的文章下方的克隆連結,將為您建立一個具有相同內容和標題以及草稿狀態的新文章。
然後你可以編輯那個新文章並給它一個新的標題(我強烈建議你這樣做 – WordPress確實允許你複製標題,但它對使用者體驗不是很好)。
它還為頁面和自定義文章型別新增了相同的克隆連結(如果您在設定中啟用它們)。
Duplicate Page
Duplicate Page外掛已經有超過90萬下載量,並且是重複的文章替代上述外掛。
安裝外掛後,轉到“文章”或“頁面”管理螢幕,或轉到自定義文章型別的管理螢幕,您會在每個文章下找到“Duplicate This”連結。這是我的自定義文章型別:

Duplicate Page外掛
單擊該連結,您將獲得一個新文章(或頁面,或自定義文章型別的文章),其內容和標題與原始文章相同,狀態為草稿。在釋出之前,您需要對其進行編輯並修改其slug。
注意:如果您使用WooCommerce,則此外掛中的Duplicate This連結將不會顯示在您的產品螢幕上。WooCommerce使用我在本文前面描述的自己的Duplicate連結覆蓋它。
如何批量複製文章或頁面
上面提到的外掛可以讓您一次複製一個文章。但是,如果您需要複製站點中的所有文章或自定義文章型別的所有文章,該怎麼辦?或者也許是你今年發表的所有文章?
WP Bulk Post Duplicator外掛可以讓你做到這一點。它沒有新增連結來複制單個文章,而是為您提供了一個設定螢幕,讓您可以根據您設定的條件批量複製文章。
注意:在撰寫本文時,此外掛已有四年未更新,因此我們建議在實際站點上使用之前將其安裝在開發站點上並對其進行測試。
安裝外掛後,轉到“設定”>“WP Bulk Post Duplicator”以檢視外掛的設定螢幕。
首先選擇要複製的文章型別以及文章狀態。因此,如果您選擇已釋出狀態的post,該外掛將複製您所有已釋出的文章。相反,如果您選擇posts和draft,它只會複製您的所有草稿文章。

批量複製設定 – 文章型別和狀態
向下滾動並選擇要複製的文章的年份(這是可選的)。然後,您還可以指定建立的新文章的狀態。如果您在此處不選擇任何內容,它將預設為Draft。

批量複製設定 – 年份和釋出狀態
單擊複製按鈕,外掛將複製您的文章。在這裡你可以看到我所有的頁面都被複制了,有12個現有的已釋出頁面和12個新的草稿頁面。然後,我需要瀏覽所有頁面並在釋出它們之前對其進行編輯。這顯然需要時間,但批量複製使速度更快,
編寫您自己的文章複製外掛
如果您不想使用第三方外掛來複制文章而寧願自己編寫程式碼怎麼辦?
沒問題。只需按照以下步驟建立您自己的文章複製外掛。
1. 設定外掛
從設定外掛開始。將一個新檔案新增到您的wp-content/plugins資料夾並將此註釋掉的文字新增到其中(您可以編輯您的檔案以反映您的作者姓名):
Plugin Name: Wbolt Duplicate posts and pages
Plugin URI: https://www.wbolt.com
Description: Plugin to accompany post on duplicating posts in WordPress. Allows for duplication of posts and pages.
Author URI: https://www.wbolt.com
<?php
/*
Plugin Name: Wbolt Duplicate posts and pages
Plugin URI: https://www.wbolt.com
Description: Plugin to accompany post on duplicating posts in WordPress. Allows for duplication of posts and pages.
Version: 1.0
Author: MK
Author URI: https://www.wbolt.com
TextDomain: wbolt
License: GPLv2
*/
<?php
/*
Plugin Name: Wbolt Duplicate posts and pages
Plugin URI: https://www.wbolt.com
Description: Plugin to accompany post on duplicating posts in WordPress. Allows for duplication of posts and pages.
Version: 1.0
Author: MK
Author URI: https://www.wbolt.com
TextDomain: wbolt
License: GPLv2
*/
2. 建立函式
現在新增複製文章的函式並將其掛接到動作鉤子上。這個動作鉤子還不存在:別擔心,我們很快就會建立它。
function wbolt_duplicate_post(){
add_action( 'admin_action_wbolt_duplicate_post', 'wbolt_duplicate_post' );
function wbolt_duplicate_post(){
}
add_action( 'admin_action_wbolt_duplicate_post', 'wbolt_duplicate_post' );
function wbolt_duplicate_post(){
}
add_action( 'admin_action_wbolt_duplicate_post', 'wbolt_duplicate_post' );
現在讓我們建立該動作,它將作為將重複連結新增到文章和頁面螢幕的功能的一部分。
function wbolt_duplicate_post_link( $actions, $post ) {
if ( current_user_can( 'edit_posts') ) {
$actions['duplicate'] = 'Duplicate';
add_filter( 'post_row_actions', 'wbolt_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'wbolt_duplicate_post_link', 10, 2);
function wbolt_duplicate_post_link( $actions, $post ) {
if ( current_user_can( 'edit_posts') ) {
$actions['duplicate'] = 'Duplicate';
}
return $actions;
}
add_filter( 'post_row_actions', 'wbolt_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'wbolt_duplicate_post_link', 10, 2);
function wbolt_duplicate_post_link( $actions, $post ) {
if ( current_user_can( 'edit_posts') ) {
$actions['duplicate'] = 'Duplicate';
}
return $actions;
}
add_filter( 'post_row_actions', 'wbolt_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'wbolt_duplicate_post_link', 10, 2);
這將新增用於複製的連結,但它實際上還不會複製任何內容。
3. 新增複製函式
讓我們新增複製程式碼。所有其餘的程式碼都在您的空wb_duplicate_post() 函式中。
首先,讓我們檢查是否選擇了有效的文章並檢查隨機數,以確保安全:
// Die if post not selected
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'wbolt_duplicate_post' == $_REQUEST['action'] ) ) ) {
wp_die( __( 'Please select a post to duplicate.', 'wbolt' ) );
if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) ) {
global $wpdb;
// Die if post not selected
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'wbolt_duplicate_post' == $_REQUEST['action'] ) ) ) {
wp_die( __( 'Please select a post to duplicate.', 'wbolt' ) );
}
// Verify nonce
if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) ) {
return;
}
global $wpdb;
// Die if post not selected
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'wbolt_duplicate_post' == $_REQUEST['action'] ) ) ) {
wp_die( __( 'Please select a post to duplicate.', 'wbolt' ) );
}
// Verify nonce
if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) ) {
return;
}
現在我們需要獲取要複製的文章的ID及其資料:
// Get id of post to be duplicated and data from it
$post_id = ( isset( $_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
$post = get_post( $post_id );
// Get id of post to be duplicated and data from it
$post_id = ( isset( $_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
$post = get_post( $post_id );
// Get id of post to be duplicated and data from it
$post_id = ( isset( $_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
$post = get_post( $post_id );
我們可以使用它來定義一個引數陣列並使用wp_insert_post()將一個新文章插入資料庫,但首先,新增一個條件語句來檢查文章是否為空:
if ( isset( $post ) && $post != null ) {
if ( isset( $post ) && $post != null ) {
}
if ( isset( $post ) && $post != null ) {
}
在該條件語句中新增以下內容:
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $post->post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
$new_post_id = wp_insert_post( $args );
// args for new post
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $post->post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
// insert the new post
$new_post_id = wp_insert_post( $args );
// args for new post
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $post->post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
// insert the new post
$new_post_id = wp_insert_post( $args );
現在我們將從舊文章中獲取分類法並將它們新增到新文章中:
// add taxonomy terms to the new post
// identify taxonomies that apply to the post type
$taxonomies = get_object_taxonomies( $post->post_type );
// add the taxonomy terms to the new post
foreach ( $taxonomies as $taxonomy ) {
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
// add taxonomy terms to the new post
// identify taxonomies that apply to the post type
$taxonomies = get_object_taxonomies( $post->post_type );
// add the taxonomy terms to the new post
foreach ( $taxonomies as $taxonomy ) {
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
}
// add taxonomy terms to the new post
// identify taxonomies that apply to the post type
$taxonomies = get_object_taxonomies( $post->post_type );
// add the taxonomy terms to the new post
foreach ( $taxonomies as $taxonomy ) {
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
}
然後我們將使用SQL查詢新增任何額外的文章後設資料:
// use SQL queries to duplicate postmeta
$post_metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if ( count( $post_metas )!=0 ) {
$sql_query = "INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value ) ";
foreach ( $post_metas as $post_meta ) {
$meta_key = $post_metas->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes( $post_metas->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query( $sql_query );
// use SQL queries to duplicate postmeta
$post_metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if ( count( $post_metas )!=0 ) {
$sql_query = "INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value ) ";
foreach ( $post_metas as $post_meta ) {
$meta_key = $post_metas->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes( $post_metas->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query( $sql_query );
}
// use SQL queries to duplicate postmeta
$post_metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if ( count( $post_metas )!=0 ) {
$sql_query = "INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value ) ";
foreach ( $post_metas as $post_meta ) {
$meta_key = $post_metas->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes( $post_metas->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query( $sql_query );
}
最後,我們檢查文章是什麼文章型別並重定向到相關螢幕。
// redirect to admin screen depending on post type
$posttype = get_post_type( $post_id );
wp_redirect( admin_url( 'edit.php?post_type=' . $posttype ) );
// redirect to admin screen depending on post type
$posttype = get_post_type( $post_id );
wp_redirect( admin_url( 'edit.php?post_type=' . $posttype ) );
// redirect to admin screen depending on post type
$posttype = get_post_type( $post_id );
wp_redirect( admin_url( 'edit.php?post_type=' . $posttype ) );
現在,在if ( isset( $post ) && $post != null )
檢查的大括號外,我們新增一條錯誤訊息:
// display an error message if the post id of the post to be duplicated can't be found
wp_die( __( 'Post cannot be found. Please select a post to duplicate.', 'wbolt' ) );
else {
// display an error message if the post id of the post to be duplicated can't be found
wp_die( __( 'Post cannot be found. Please select a post to duplicate.', 'wbolt' ) );
}
else {
// display an error message if the post id of the post to be duplicated can't be found
wp_die( __( 'Post cannot be found. Please select a post to duplicate.', 'wbolt' ) );
}
就是這樣!
完整外掛
這是外掛的完整程式碼:
/* Plugin Name: Wbolt Duplicate posts and pages
Plugin URI: https://www.wbolt.com
Description: Plugin to accompany post on duplicating posts in WordPress. Allows for duplication of posts and pages. Version: 1.0 Author: Rachel McCollin
Author URI: https://www.wbolt.com
/********************************************************************
wbolt_duplicate_post() - duplicates the selected post
*********************************************************************/
function wbolt_duplicate_post(){
// Die if post not selected
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'wbolt_duplicate_post' == $_REQUEST['action'] ) ) ) {
wp_die( __( 'Please select a post to duplicate.', 'wbolt' ) );
if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) ) {
// Get id of post to be duplicated and data from it
$post_id = ( isset( $_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
$post = get_post( $post_id );
if ( isset( $post ) && $post != null ) {
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $post->post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
$new_post_id = wp_insert_post( $args );
// add taxonomy terms to the new post
// identify taxonomies that apply to the post type
$taxonomies = get_object_taxonomies( $post->post_type );
// add the taxonomy terms to the new post
foreach ( $taxonomies as $taxonomy ) {
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
// use SQL queries to duplicate postmeta
$post_metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if ( count( $post_metas )!=0 ) {
$sql_query = "INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value ) ";
foreach ( $post_metas as $post_meta ) {
$meta_key = $post_metas->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes( $post_metas->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query( $sql_query );
// redirect to admin screen depending on post type
$posttype = get_post_type( $post_id );
wp_redirect( admin_url( 'edit.php?post_type=' . $posttype ) );
// display an error message if the post id of the post to be duplicated can't be found
wp_die( __( 'Post cannot be found. Please select a post to duplicate.', 'wbolt' ) );
add_action( 'admin_action_wbolt_duplicate_post', 'wbolt_duplicate_post' );
// Add the duplicate link to action list for post_row_actions
function wbolt_duplicate_post_link( $actions, $post ) {
if ( current_user_can( 'edit_posts') ) {
$actions['duplicate'] = 'Duplicate';
add_filter( 'post_row_actions', 'wbolt_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'wbolt_duplicate_post_link', 10, 2);
<?php
/* Plugin Name: Wbolt Duplicate posts and pages
Plugin URI: https://www.wbolt.com
Description: Plugin to accompany post on duplicating posts in WordPress. Allows for duplication of posts and pages. Version: 1.0 Author: Rachel McCollin
Author URI: https://www.wbolt.com
TextDomain: wbolt
License: GPLv2
*/
/********************************************************************
wbolt_duplicate_post() - duplicates the selected post
*********************************************************************/
function wbolt_duplicate_post(){
global $wpdb;
// Die if post not selected
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'wbolt_duplicate_post' == $_REQUEST['action'] ) ) ) {
wp_die( __( 'Please select a post to duplicate.', 'wbolt' ) );
}
// Verify nonce
if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) ) {
return;
}
// Get id of post to be duplicated and data from it
$post_id = ( isset( $_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
$post = get_post( $post_id );
// duplicate the post
if ( isset( $post ) && $post != null ) {
// args for new post
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $post->post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
// insert the new post
$new_post_id = wp_insert_post( $args );
// add taxonomy terms to the new post
// identify taxonomies that apply to the post type
$taxonomies = get_object_taxonomies( $post->post_type );
// add the taxonomy terms to the new post
foreach ( $taxonomies as $taxonomy ) {
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
}
// use SQL queries to duplicate postmeta
$post_metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if ( count( $post_metas )!=0 ) {
$sql_query = "INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value ) ";
foreach ( $post_metas as $post_meta ) {
$meta_key = $post_metas->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes( $post_metas->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query( $sql_query );
}
// redirect to admin screen depending on post type
$posttype = get_post_type( $post_id );
wp_redirect( admin_url( 'edit.php?post_type=' . $posttype ) );
} else {
// display an error message if the post id of the post to be duplicated can't be found
wp_die( __( 'Post cannot be found. Please select a post to duplicate.', 'wbolt' ) );
}
}
add_action( 'admin_action_wbolt_duplicate_post', 'wbolt_duplicate_post' );
// Add the duplicate link to action list for post_row_actions
function wbolt_duplicate_post_link( $actions, $post ) {
if ( current_user_can( 'edit_posts') ) {
$actions['duplicate'] = 'Duplicate';
}
return $actions;
}
add_filter( 'post_row_actions', 'wbolt_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'wbolt_duplicate_post_link', 10, 2);
<?php
/* Plugin Name: Wbolt Duplicate posts and pages
Plugin URI: https://www.wbolt.com
Description: Plugin to accompany post on duplicating posts in WordPress. Allows for duplication of posts and pages. Version: 1.0 Author: Rachel McCollin
Author URI: https://www.wbolt.com
TextDomain: wbolt
License: GPLv2
*/
/********************************************************************
wbolt_duplicate_post() - duplicates the selected post
*********************************************************************/
function wbolt_duplicate_post(){
global $wpdb;
// Die if post not selected
if (! ( isset( $_GET['post']) || isset( $_POST['post']) || ( isset($_REQUEST['action']) && 'wbolt_duplicate_post' == $_REQUEST['action'] ) ) ) {
wp_die( __( 'Please select a post to duplicate.', 'wbolt' ) );
}
// Verify nonce
if ( ! isset( $_GET['duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) ) {
return;
}
// Get id of post to be duplicated and data from it
$post_id = ( isset( $_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
$post = get_post( $post_id );
// duplicate the post
if ( isset( $post ) && $post != null ) {
// args for new post
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $post->post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title,
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order
);
// insert the new post
$new_post_id = wp_insert_post( $args );
// add taxonomy terms to the new post
// identify taxonomies that apply to the post type
$taxonomies = get_object_taxonomies( $post->post_type );
// add the taxonomy terms to the new post
foreach ( $taxonomies as $taxonomy ) {
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
wp_set_object_terms( $new_post_id, $post_terms, $taxonomy, false );
}
// use SQL queries to duplicate postmeta
$post_metas = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if ( count( $post_metas )!=0 ) {
$sql_query = "INSERT INTO $wpdb->postmeta ( post_id, meta_key, meta_value ) ";
foreach ( $post_metas as $post_meta ) {
$meta_key = $post_metas->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes( $post_metas->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query( $sql_query );
}
// redirect to admin screen depending on post type
$posttype = get_post_type( $post_id );
wp_redirect( admin_url( 'edit.php?post_type=' . $posttype ) );
} else {
// display an error message if the post id of the post to be duplicated can't be found
wp_die( __( 'Post cannot be found. Please select a post to duplicate.', 'wbolt' ) );
}
}
add_action( 'admin_action_wbolt_duplicate_post', 'wbolt_duplicate_post' );
// Add the duplicate link to action list for post_row_actions
function wbolt_duplicate_post_link( $actions, $post ) {
if ( current_user_can( 'edit_posts') ) {
$actions['duplicate'] = 'Duplicate';
}
return $actions;
}
add_filter( 'post_row_actions', 'wbolt_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'wbolt_duplicate_post_link', 10, 2);
恭喜!您現在擁有自己的外掛來複制WordPress網站中的文章和頁面。
小結
使用外掛複製文章和頁面比手動複製更快,並且可以消除複製內容時出錯的風險。
如果您只有一篇文章要複製,您可能會發現使用WordPress中內建的“複製所有內容”功能會更快,但對於不止一篇文章,使用外掛是值得的。如果您使用上面列出的外掛之一,或者自己編寫程式碼,您將能夠根據需要複製任意數量的文章、頁面或自定義文章型別。
如果你這樣做,你會節省時間(更重要的是),當你應該編輯重複的文章時,你將消除意外編輯原始文章的風險。
評論留言