How to Duplicate a Post or a Page in WordPress
You very likely know the pain of duplicating content across posts or pages when you’re working with similar structures and want to maintain consistent formatting. Typing it all out manually can be painful drudgery, and even with copying and pasting, you risk introducing mistakes.
Fortunately, content duplication doesn’t need to be so complicated. WordPress provides multiple methods – both built-in and via plugins – to create new posts and pages that are identical to your existing ones.
In this article, we’ll briefly discuss why you might need to duplicate a page or post in WordPress. We’ll then walk through how to do so with and without a plugin. Let’s dive in!
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
There are several plugins available for duplicating posts, pages, and custom post types. Some popular options include Duplicate Page and Post, Duplicate Post, and Duplicate Page.
Despite their confusing names, these plugins all work very similarly and can clone multiple post types. We’ll show you how to use Duplicate Page and Post as an example.
First, you’ll want to install and activate the plugin. Once this is done, a Duplicate link will be added for each post or page in their respective lists in your WordPress dashboard:
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
There’s one more way to add duplication functionality to your site without a plugin. This method involves editing your theme. If you’d prefer not to install yet another plugin on your site but need to be able to copy metadata between your posts, this might just be the solution for you.
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:
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'] = '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);
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:
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
You may want to duplicate your pages or posts to create templates for future content or to back up posts before making alterations to your website. No matter your reason, however, there are simple methods for getting it done.
To recap, here are three way you can clone a post or duplicate a page in WordPress:
- Duplicate content without a plugin using the Copy all content link in the Block Editor.
- Use a plugin such as Duplicate Page and Post, Duplicate Post, or Duplicate Page.
- Add duplication functionality by editing the functions.php file of your current theme.
Do you have any questions about duplicating pages and posts in WordPress? Let us know in the comments section below!
The post How to Duplicate a Page in WordPress (3 Methods) appeared first on Torque.
https://wiredgorilla.com/%e2%80%8bhow-to-duplicate-a-page-in-wordpress-3-methods/
Comments
Post a Comment