How To Duplicate A Page In WordPress

Deal Score0
Deal Score0

When you are running a blog or website, there might come a time when you will have to copy a page or post quickly. For example, you might want to repost a sales product page that already has the necessary information or a blog post that you want to refresh by adding new material.

While you can manually do this by copying and pasting the content of the page into a new post, you might be losing a lot of data like SEO settings, featured image, post settings, and metadata. You would have to go back and recreate all of those things, which will take valuable time. Since time is money, here is a more efficient way to duplicate a page and post while keeping its content and settings intact.

Duplicate a WordPress Post or Page Using A Plugin

For those who are nervous about doing it manually, you’ll be able to find a plugin that can help you with duplicating a page. One example is the Yoast Duplicate Post plugin. It has more than three million downloads and is consistently updated to ensure that your website stays safe and secure. The app makes it possible to clone any type of post with a single click.

To download it, visit the Plugins → Add New page within your WordPress admin dashboard. Type in the name of the plugin or the function of the plugin you are looking for and click “Install Now.” Once you’ve installed it, make sure to activate it; otherwise, the plugin won’t work.

Once you’ve installed it, you can then start to use the plugin. For the Yoast Duplicate Post plugin, you will simply go to PostsAll Posts within your WordPress dashboard. Now, you’ll see that there are two additional options available: Clone and New Draft.

If you select “Clone,” it will create a duplicate post and copy over all of your settings and configurations, but it won’t open that new draft in the post editor. This might be a good option if you want to clone multiple posts at once without making edits.

Or, you can select “New Draft,” which will clone the post and will also open it in the post editor so you can start making changes to the page immediately.

If you would like to use the plugin to clone pages, you’ll find the same steps apply.

This type of plugin is especially perfect for people who want to use it to copy landing pages or product pages, or redesign a website. However, if you don’t want to use a plugin, there are other options available.

Duplicate a WordPress Post or Page Without A Plugin

Option 1: Edit the code within the functions.php file

You can easily copy a post or page of your website by editing the code within the functions.php file. This isn’t difficult, but if you don’t have any coding experience, it might be a good idea to use a plugin option instead. Even if you do have the experience, you should always make a backup of your website first. If something goes wrong and you accidentally make a mistake, it will save you hours of work rebuilding your website if you have a backup already ready to go. You should get in the habit of making a backup of your website regardless of whether or not you are fiddling with the code because you want to have a clean copy of the site you can pop in as a backup to restore your site.

Once you have identified which post you want to clone, you will need to add this 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 and 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 clone WordPress pages, you’ll use the same code, but you’ll replace the last line of the code with this line:

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

  1. Copy and Paste Code to Clone The Page 

If editing code with the functions.php file is too intimidating, there are other ways. You can copy and paste the code of the WordPress page or post you want to duplicate by following these steps:

First, open the post or page you want to close. Select the More Tools & Options menu and then Code Editor. Copy the code of the post you want to duplicate and select New Page or New Post. Once the new page or post opens, open the Code Editor and paste the code in the page you want to clone. Again, click More tools & Options and then Visual Editor. Now, you will have a duplicate of your desired page!

As you can notice, this method is a little more time consuming than using a plugin. If you have multiple pages or posts you want to close on your WordPress site, it will likely be a good idea to use a plugin instead.

Final Thoughts

Duplicating your WordPress pages or posts doesn’t have to be difficult or scary. Using the method that you prefer can help you get your dream site up and running in no time.

We will be happy to hear your thoughts

Leave a reply

10 Best Hosting Website
Login/Register access is temporary disabled