Beginner’s Guide: How to duplicate a page in WordPress or a post in WordPress?

comment dupliquer une ou un article WordPress

In this article, you will first learn why you may need to duplicate a page in WordPress or a post in WordPress.

And then you’ll see how to do it, either by touching the WordPress source code or by simply doing it with a few clicks.

Why clone or duplicate a page in WordPress or duplicate a WordPress post?

pourquoi dupliquer une page wordpress

THE FOLLOWING AFTER THIS AD

Why duplicate a WordPress page or duplicate a WordPress post, some may ask.

There are several instances where duplicating a WordPress page or post would be beneficial.

When designing a WordPress website, it is prudent to duplicate the pages of the imported WordPress theme before customizing them.

Or, you have a blog where you want to update old posts by replacing obsolete elements with new content.

Duplicating these old posts before updating them, allows you to keep the post’s featured page image, post metadata, optimized keywords for the post. You will no longer have to manually enter all this information.

It’s also the same for a sales page, or a product page, duplicating them before any addition or update allows you to keep their content and their parameters.

To do this, you have the choice between using an extension or touching the WordPress source code.

Duplicate a page in WordPress or duplicate a post in manually

comment dupliquer une page WordPress

This method is the most complex, but it is preferred by some because not installing an additional extension avoids :

  • A potential conflict with other extensions already installed on the site
  • Potential overloading of the site and thus contribute to the fast loading of the site

This method requires you to get your hands dirty.

Indeed, you will have to enter some code in one of the WordPress files to duplicate a page in WordPress or duplicate a post in WordPress.

Enable cloning or duplicating a page and a post within WordPress

Before you can clone or duplicate a WordPress page or post without the use of an extension, you must first enable the cloning feature within WordPress.

To do this, you need a file transfer program like Filezilla to download the WordPress file to be modified, and then once the modification is done, upload the file to the server.

You will also need a code editor to add the activation code to the file.

PS: You should make a backup of your site well before starting this manipulation, just to be safe.

Look for the functions.php file in WordPress and download it.

Using your text editor, add the following code at 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'] = '<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 );

Thanks to this code, you have activated the duplicate of posts, to activate the duplicate of pages, repeat the same code, but replace the last line with :

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

Save your file with CRTL+S and upload the file to your server.

Now that the cloning feature is enabled, you can duplicate a WordPress page or post.

Duplicate a WordPress page or post using a plugin

If you’re worried about messing with the functions.php file, or if you just don’t want to mess with the code, you can use a plugin to duplicate your WordPress pages and posts.

The advantage of using a duplicate plugin is that it makes your life easier and saves you from having to touch the source code of your website yourself.

There are many of them, but in this article we will only suggest a few.

The ones proposed below are regularly updated.

Yoast Duplicate Post

yoast duplicate post : Duplicate a page in wordpress plugin

Yoast Duplicate Post is installed on nearly 5 million WordPress websites to this date.

It allows you to duplicate any type of posts, copy them into new drafts for later editing.

It also offers a prefix or suffix option, to differentiate your original post and the clone.

To use Yoast Duplicate Post, here’s how to do it:

On the WordPress admin page showing your site’s posts or pages, you can click the “Clone” link under the post or page title, this will immediately create a copy and return to the list.
You can also select one or more items, then choose “Clone” from the “Grouped Actions” dropdown to clone them all at once.

This plugin offers even more features that you can discover on its presentation page.

Duplicate Page and Post

duplicate page and post : Duplicate a page in wordpress plugin

Duplicate Page and Post is also very popular. Its users number in the hundreds of thousands.

Here are some of its features:

  • Create a clone of a particular page.
  • Create a clone of a particular post.
  • Create a clone of a particular custom post
  • Option to select the editor
  • Option to add a suffix.
  • Option to add a custom text for the duplicate link button.
  • Option to select the status of the duplicate items.
  • Option to redirect after clicking Duplicate.

Duplicate Page

duplicate page : Duplicate a page in wordpress plugin

Duplicate Page is installed on more than two million WordPress websites. It offers additional features that some other cloning plugins do not.

It allows you to duplicate posts, pages and custom posts. In addition, you can save the resulting copies as draft, pending, public or private.

Post Duplicator

post duplicator : Duplicate a page in wordpress plugin

The designer of Post Duplicator created it for his own needs, but considering the usefulness of his tool, he decided to make it available to the WordPress community.

At this moment it is installed and active on more than 200,000 WordPress sites.

It allows to reproduce an exact copy of a selected posts. It takes into account custom posts, as well as taxonomies and custom fields.

Once it is installed and active on your site, simply hover over an post in the WordPress editing area and select “Duplicate” to create a duplicate post.

Other WordPress resources available on the blog

19 Best Hosting WordPress With Amazing Features

15 Best Portfolio WordPress Themes to Showcase Your Works and Projects

20 Best Elementor Themes for a successful Web Project

20 Top Digital Agency WordPress Themes with Great Features

20 Best Woocommerce themes for ecommerce

15 Best Multi-Vendors WordPress Themes

15 Best Marketplace WordPress Themes

40+ best responsive themes (wordpress, woocommerce, shopify, prestashop, drupal)

Leave a Reply