How to duplicate a page in WordPress

How to duplicate a page in WordPress - CodeFlist

Do you know how to duplicate a page in WordPress? In this post, you will learn how to copy a page in WordPress.

Why copy a page in WordPress?

There are different instances when you need to know how to copy a page in WordPress.

For example, you can rewrite your old article to publish a new post or if there are multiple information on a sales page, you can copy the useful contents.

You may have a question, Can’t we copy these contents manually?

Of course, you can copy them manually. However, the manual copy will not copy the SEO settings, Post settings, associated metadata, and the featured image.

So, isn’t it time-saving if you can copy all those settings in a single click?

If you learn how to duplicate a page in WordPress, you can just edit the content and begin working on it.

In this post, you will know how to copy a page in WordPress.

How to duplicate a page in WordPress?

You can duplicate a page in WordPress either with the help of plugin or without a plugin. Let’s look at the plugin approach at the beginning:

Duplicate a page in WordPress with a plugin

At first, you need to install and activate the Yoast Duplicate Post plugin in WordPress.

After you install the plugin, Open Pages > All Pages.

You will find Clone and New Draft option in the existing pages.

How to clone page in WordPress

Click on the clone if you want to create a copy of the page. Click New Draft, if you want to create a duplicate page and to open it in the post editor to start editing.

In a similar way, if you want to create a clone or New Draft for a post then go to Posts > All Posts and select your desired post.

This feature will be useful to you in many ways if you want to copy a particular design from one page or post to another and even you can copy landing pages which will be useful for different marketing campaigns.

While duplicating a page or a post, you can enable user roles and also set custom post types.

You can observe the different settings for the plugin from the image below:

How to copy a page in WordPress - What To Copy
What To Copy

As in this picture, the first tab shows What to Copy. There are default options provided by the plugin. However, you can enable or disable particular settings based on your preference.

Have a look at the permissions tab.

How to duplicate a page in WordPress - Permissions
Permissions

In the permissions tab, you can see the default roles that are allowed to copy and also the default post types that can be copied. You can change these settings if you are an administrator.

Now Let’s have a look at the display tab.

Yoast Duplicate Post Settings display
Display

The display tab lists the default display settings for the plugin. As previous tabs, you can change it accordingly. After you change the settings, do not forget to save them.

Not only Yoast Duplicate Post, but you can also find other plugins like Duplicate Page and Post, Post Duplicator, etc. in the official WordPress plugins repository that you can use for the same purpose.

How to Duplicate a page in WordPress without a plugin?

You can also duplicate the page in WordPress with the help of coding.

With this method, you can avoid the addition of a plugin.

Add this code in your functions.php file. To access the functions.php file, you can use File Manager, FTP Client, or Edit Appearance > Theme Editor > Theme Functions.

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!');
  }
   if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
    return;
 
  $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
 
  $post = get_post( $post_id );
   $current_user = wp_get_current_user();
  $new_post_author = $current_user->ID;
 
  if (isset( $post ) && $post != null) {
 
    $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
    );
 
    $new_post_id = wp_insert_post( $args );
 
    $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);
    }
 
    $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);
    }
 
 
    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' );
 
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 ); 

If you want to change the filter name on the last line as shown below:

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

If you correctly embed this code, you will see a Duplicate option across all posts and pages.

Conclusion

Hopefully, you have learned how to duplicate a page in WordPress. We mentioned several plugins with the help of which you can copy a page in WordPress. You can even copy a page without the plugin, with the help of code. In a similar manner, you can duplicate a post in WordPress.

Additional Reading

  1. How to delete WordPress site
  2. WordPress 5.5 Beta 1
  3. How to increase website traffic on Google