19 June 2023
Looking for an easy way to duplicate a page or post in WordPress?
In the world of website development and content management, WordPress stands out as a powerful and user-friendly platform. It offers a number of features that simplify the process of managing and organizing your website’s pages. One such feature is the ability to duplicate a page, which can save you time and effort when creating similar or related content.
In this article, we’ll discuss why you might need to duplicate a page in WordPress. We’ll then walk through how to do it with and without a plugin.
Let’s dive in!
Duplicating a page in WordPress offers several advantages and serves various purposes. Here are some compelling reasons why you might want to copy a page in WordPress:
There are three easy ways to duplicate a WordPress page, post, or any custom post type:
Let’s go through them.
There are a number of WordPress plugins available that you can use to duplicate pages on WordPress. For this article, we will use Yoast Duplicate Post.
This plugin is great if you just want a quick and easy way to clone pages, posts, or any other custom post type.
To get started, install and activate the Yoast Duplicate Post plugin. For more, check out our guide on how to install WordPress plugins.
Upon activation, go to the Pages or Posts area in your WordPress dashboard.
Whether you’re working with pages or posts, the Duplicate Post plugin adds three links below each title by default:
However, there are cases in which you would like to copy all the meta-data or even limit the user who has access. Fortunately, you can do all of this (and more) using the Duplicate Post settings.
The duplicate post plugin works right away and supports posts and pages by default.
However, you can also configure the plugin to support custom post types, limit user roles, and choose what to copy when creating a duplicate.
Let’s take a look at each of these options.
Go to Settings » Duplicate Post page to configure plugin settings.
The settings page is separated into three tabs. The What to Copy tab lets you choose what to copy when creating a duplicate.
The default settings should be enough for most websites. You may, however, check items you want to duplicate and uncheck stuff you don’t want to be copied.
Following this, go to the Permissions tab.
By default, the plugin allows administrators and editors to make duplicate posts.
It also allows the duplicate post feature for posts and page post types.
If you use custom post types on your website, they will display here. You may select whether to allow the duplicate post option for the custom post type as well.
Finally, go to the Display tab and select where you want the clone post links to appear. By default, the plugin displays them on the post list, edit screen, and admin bar.
Once you are done with your settings, click on the Save Changes button to save your settings.
This method uses the built-in duplication feature that is available by default in the WordPress block editor (Gutenberg).
Simply go to the Pages » All Pages and click on the page you want to copy.
Once open, click on the three-dot menu in the top-right corner of the screen. Now, select the Copy all blocks option:
Next, go back and create a new page and paste the copied content into it.
After pasting the content, your duplicated page will be ready to publish.
Note that this method does not copy over metadata such as the post’s title, taxonomies (categories and tags), etc. However, if you’re just creating templates for future content, it can work just fine.
If you prefer a do-it-yourself approach, you may also write your own duplicate page function using custom code. This approach has no advantage over the plugin route other than avoiding the need to install another plugin on your site.
To use this code, add it to your theme’s functions.php file or a code management plugin like Code Snippets. If you add it to the functions.php file, make sure to use a child theme.
$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'] = 'ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate';
}
return $actions;
}
add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );
add_filter( 'page_row_actions', 'rd_duplicate_post_link', 10, 2 );
By default, the snippet allows duplication for both posts and pages. If you just want to activate it for one of the two, delete the page_row_actions or post_row_actions filter at the bottom of the code line.
That’s all!
We hope this article helped you learn how to easily duplicate a WordPress page.
For more, check out these other helpful resources:
Lastly, if you like this article, please follow us on Facebook and Twitter.