Easily Duplicate a Page or a Post in WordPress

Creating a duplicate of your existing WordPress posts or pages can be useful in many situations. You can use the copy as a template for future posts, or a reference when you redesign your website. Typing it all out manually can be painful drudgery, and even with copying and pasting, you risk introducing mistakes.

Thankfully, there are multiple ways to clone a page or post in WordPress. This tutorial will explain those different methods

Why You’d Want to Duplicate a Page or Post in WordPress

There are some common scenarios where you’d need to duplicate a page or post in WordPress. One of the most common is that you want to create templates for future content on your blog.

For example, if your posts generally use the same structure and formatting, you might choose to create a template, especially if the style is particularly complex or nuanced. All you’d need to do for subsequent posts is add text, images, and any other necessary content.

Alternatively, you may also need to back up a post or page before making alterations to it. For instance, say you’ve just completed a design job for a client and they want to check it out and make changes to better suit their taste.

It’s a smart practice to back up all your work before that happens. For one thing, it will give you a reference for future assignments. Plus, if your client decides they’d like you to revert their changes or accidentally deletes something important, you’ll be able to restore the original.

Similarly, duplicate pages also provide you with references for when you redesign specific areas of your website. That said, if you’re doing a complete overhaul or if your intention is a bulk migration of content from one site to another, a full site backup might be in order.

One important thing to note is that having duplicate pages or posts on your site isn’t beneficial for Search Engine Optimization (SEO). Therefore, you’ll want to avoid publishing the same exact content twice to avoid any issues.

How to Duplicate a Page or Post in WordPress (3 Methods)

As we mentioned earlier, duplicating posts and pages can be done with or without plugins. Here are a few methods you can try, depending on your goals and skill level.

1. Use the Block Editor to Duplicate Content

This method uses the built-in duplication mechanism that is accessible from the Block Editor. Start by opening the page or post you want to copy.

Next, click on the three-dot menu in the top-right corner of the screen. Select the Copy all content option:

You can then create a new post and simply paste the copied content into it.

Note that this method does not copy over metadata such as the post’s title, taxonomies (categories and tags), or SEO data such as keywords and meta descriptions. However, if you’re just creating templates for future content, it can work just fine.

2. Duplicate Posts and Pages Using a Plugin

Duplicate Page and Post enables you to clone WordPress pages and posts quickly. The plugin can duplicate a page or post without changing its content, title, or style.

Using this plugin is relatively easy. You just need to follow these steps:

  1. After installing and activating the plugin, go to the All Pages or All Posts menu, depending on what you want to duplicate.
  2. Hover over the post or page you want to clone and click Duplicate.
  3. The cloned post or page will appear as a new draft with the same name as the original. Open the copy to edit the content

A new post or page with the same title is created as a draft when you click on the Duplicate option. All the content and metadata are copied over as well.

3. Add a ‘Duplicate’ Option to Your Theme

We strongly recommend to backup your website before editing any WordPress files.

Besides using plugins, you can also tweak some codes to duplicate WordPress pages and posts. While this method sounds like a bother, it is actually quite simple.

Before you move forward with this method, make sure to create a backup of your site. We also recommend using a child theme. This ensures that you won’t lose the duplication functionality when you update your theme in the future.

We’ll be using the WordPress built-in Theme Editor to make changes to the functions.php file:

Screenshot of the WordPress Theme Editor.

However, you can also access it via File Transfer Protocol (FTP) using Filezilla. Either way, you’ll need to add the following code 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 ); add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);

The last two lines enable the duplication functionality for both posts and pages. If you decide you want this feature for just one or the other, you can remove the relevant line.

You should now see a Duplicate option alongside other functionality for posts and pages in their respective lists:

Screenshot of WordPress Posts screen

This Duplicate feature is a little different from the one we added with a plugin above. When you click on the link here, you’ll notice it opens the new draft in the WordPress editor so you can start working on it immediately.

Conclusion

We hope this tutorial answers all your questions about how to duplicate a post or page in WordPress. To refresh our minds, let’s have a quick recap.

  1. Duplicate content without a plugin using the Copy all content link in the Block Editor.
  2. Use a plugin such as Duplicate Page and Post, Duplicate Post, or Duplicate Page.
  3. Add duplication functionality by editing the functions.php file of your current theme.
Spread the love

Posted by News Monkey

blank