Easy Way to Duplicate a Page or Post in WordPress

06Aug, 2020

Duplicating a page or post in WordPress can mean more than simply copying and pasting the content. You can also retain the page template, SEO data, and images, in order to save time when redesigning your website or updating your content.

Fortunately, duplicating pages and posts and all their associated data in WordPress can be easy. There are simple ways to get the job done, both with and without a plugin.

In this article, we’ll look at how to clone a page or post safely, and introduce some plugins that can help. Let’s jump right in!

Easily Clone a Page in WordPress with These Plugins

Cloning a page in WordPress is very simple when you use a plugin, because everything is done right in your dashboard. Plugins are also the safest way to duplicate a post or page, since you won’t be modifying your site’s code directly.

If you’re looking for the right tool, here are four plugins that are worth checking out.

1. Duplicate Post

One of the go-to options for WordPress page and post cloning is Duplicate Post. This plugin is easy to use, and clones everything from the content of the page or post to the associated comments. It also offers a prefix or suffix option, to differentiate your original post and the clone.

To duplicate a post with this tool, you just need to:

  1. Install and activate the plugin.
  2. In your WordPress dashboard, go to Posts > All when cloning posts, or Pages > All when cloning pages.
  3. Navigate to the page or post you want to copy, and click on Clone to duplicate it.
  4. Multiple pages or posts can be selected, and you can clone them all at once using Bulk Actions.

2. Duplicate Page and Post

Duplicate Page and Post does not have a lot of features, but makes up for that in speed. This lightweight plugin is one of the fastest ways to clone a post or page in WordPress, and won’t weigh your site down with unnecessary bells and whistles.

To clone a page or post with this plugin, use the following steps:

  1. Install and activate the plugin.
  2. Go to Posts > All or Pages > All, depending on what you want to duplicate.
  3. Hover over the page or post you want to clone.
  4. Click on the Duplicate option.

3. Duplicate Page

Duplicate Page offers a few additional features that some other cloning plugins don’t provide. This plugin will duplicate posts, pages, and custom post types. Plus, you can save the resulting copies as drafts, pending, public, or private.

To use Duplicate Page, you just need to:

  1. Install and activate the plugin.
  2. Configure its settings to meet your needs.
  3. Go to Pages > All or Posts > All to find the content you want to duplicate.
  4. Click on the Duplicate This option.

4. Post Duplicator

Another simple cloning plugin is Post Duplicator. This solution creates an exact duplicate of any post or page, including custom post types, custom fields, and custom taxonomies. It’s quick and easy to use, and shouldn’t add much weight to your site.

To duplicate content with this tool, follow these steps:

  1. Install the plugin and activate it.
  2. Navigate to Posts > All or Pages > All to find the content you want to clone.
  3. Hover over the post or page.
  4. Click on the Duplicate Page or Duplicate Post option.

Duplicating a Page in WordPress Without a Plugin

Of course, you don’t have to use a plugin to clone a page or post in WordPress. This can also be done manually, by either editing the funtions.php file or copying and pasting the relevant code. Let’s look at how both methods work.

1. Enable Cloning via funtions.php Code

One of the manual ways to clone a WordPress page or post is to edit the code in your functions.php file. While this can be easy to do, you do need to be cautious and make a backup of your website first.

To enable cloning for posts, you’ll need to access your functions.php file and open it for editing, using Secure File Transfer Protocol (FTP) or whatever other method you prefer. Then you’ll need to add the following code snippet to the end of the file:

/*
* Function for post duplication. Dups appear as drafts. User is redirected to the edit screen
*/
function rd_duplicate_post_as_draft(){
global $wpdb;
if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
wp_die('No post to duplicate has been supplied!');
}
/*
* Nonce verification
*/
if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
return;
/*
* get the original post id
*/
$post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
/*
* and all the original post data then
*/
$post = get_post( $post_id );
/*
* if you don't want current user to be the new post author,
* then change next couple of lines to this: $new_post_author = $post->post_author;
*/
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
/*
* if post data exists, create the post duplicate
*/
if (isset( $post ) && $post != null) {
/*
* new post data array
*/
$args = array(
'comment_status' => $post->comment_status,
'ping_status'    => $post->ping_status,
'post_author'    => $new_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 post by wp_insert_post() function
*/
$new_post_id = wp_insert_post( $args );
/*
* get all current post terms ad set them to the new post draft
*/
$taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
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);
}
/*
* duplicate all post meta just in two SQL queries
*/
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos)!=0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
if( $meta_key == '_wp_old_slug' ) continue;
$meta_value = addslashes($meta_info->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);
}
/*
* finally, redirect to the edit post screen for the new draft
*/
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die('Post creation failed, could not find original post: ' . $post_id);
}
}
add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );
/*
* Add the duplicate link to action list for post_row_actions
*/
function rd_duplicate_post_link( $actions, $post ) {
if (current_user_can('edit_posts')) {
$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
}
return $actions;
}
add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );

To enable cloning for pages as well, use the same code but replace the final line with:

add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);

After that, you can save the file and reupload it to your server. Then you can head back to your WordPress dashboard. A Duplicate button should now appear when you hover over a page or post you want to clone.

2. Manually Copy & Paste Code to Duplicate a Page

If you do not want to edit your functions.php file, you can manually copy and paste the code for the page or post you want to clone. To do this, you will need to:

  1. Open the page or post you want to duplicate.
  2. Click on the More Tools & Options menu.
  3. Select Code Editor.
  4. Copy the code for the page or post.
  5. Click on New Post or New Page.
  6. In the new post or page, open the Code Editor.
  7. Paste in the code.
  8. Click on the More Tools & Options menu.
  9. Select Visual Editor.
  10. The new page or post should now be a clone of the old one.

This process can take a little time, and you’ll need to do it individually for each page or post you want to copy. That’s why we recommend using a plugin if you’re looking to duplicate a lot of content.

error: Content is protected !!