How to Duplicate a Page or Post in WordPress – (2022)
Last updated on November 25th, 2022 by Max OLooking for a way to duplicate a page or post in WordPress?
When running a blog it might be useful to quickly duplicate a post. There may be content, paragraphs, images, video or featured images from an older post that you may want to reuse, or you just want to create a variation of an existing page and keep the original version in case you want to switch back.
In this article we will explore several ways we can duplicate a page/post, with and without a plugin. Lets get right into it.
Duplicate Post Plugin
Video Tutorial
Duplicate Post is a popular WordPress plugin for cloning pages and posts. To download it go to Plugins > Add New and search for ‘Duplicate Post’ in the searchbar.
After activating the plugin, you will want to go to Menu > Copy & Delete Posts on the left-hand side.
From here, you will see all the plugin settings such as the configuring the elements you want to copy, the names of the copies as well as settings for deleting duplicate posts or pages.
You can specify which fields you want to copy or omit, such as:
- title
- slug
- excerpt
- content
- featured image
- author
- template
- password
- date
- status
Now go to Posts > All Posts. You will notice that there’s now a link to copy underneath the post title. Click on ‘Copy’ to duplicate the page.
A cloned version of the page will appear with the same name. You can now open the draft and edit it.
You can also copy a page while you’re in the Guternberg editor. On the right hand sidebar menu you will see a ‘Copy this’ button. Clicking on that will also make a copy of the page.
The copy button also appears on the top admin bar.
Yoast Duplicate Post
Duplicate Post plugin from Yoast is also another popular choice for cloning pages and posts. This plugin allows you to copy pages and posts of any type.
To duplicate a page/post, just follow these simple steps:
- Download and Activate the Yoast Duplicate Post plugin
- Go to Posts > All Posts or Pages > All Pages (if you want to clone a page)
- Below the page/post title click on ‘Clone’, this will immediately create a copy of the page.
- You can also click on the ‘New Draft’ link which will lead you to the edit page of the new draft
Duplicate a Post in WordPress Without a Plugin
If you don’t want to use a plugin, you can also duplicate a post by adding some code to your functions.php file.
To edit your functions.php file you can go directly into cPanel and edit it there, or if you don’t have access, you can use an FTP client such as FileZilla.
Paste the following code snippet into your functions.php file and save your changes.
[php]
/*
* 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 );[/php]
To enable cloning for pages, use the same code but replace the last line with
[php]add_filter(‘page_row_actions’, ‘rd_duplicate_post_link’, 10, 2);[/php]
After that, go back to your WordPress Dashboard > Posts > All Posts. A duplicate button will now appear when you hover over a page or post.
We hope you found this article useful and that you learned how to clone a page or post in WordPress. Feel free to leave a comment if you have any additional questions, Thanks.