TECHNIG
Gateway for IT Experts and Tech Geeks

How to Duplicate Page in WordPress?

If you have ever faced a situation where you want to duplicate WordPress page or post with all the structure, SEO, and design, you can do that easily. In this article, you will learn the best way to duplicate WordPress page or post with and without a plugin.

How to Duplicate page in WordPress with Plugin

let’s imagine that you are not a developer. You can use the following plugins to duplicate page in WordPress quickly.

There are many plugins out there that let you duplicate post and pages. We recommend the most updated and applicable with the latest version of WordPress.

How to duplicate WordPress page or post with plugin
How to duplicate WordPress page or post with plugin

Duplicate Post 

WordPress Duplicate Post By Enrico Battocchi is the most recommended plugin on WordPress plugin directory. Once you install and activate this plugin, you can go to any post type like pages or posts. You will see two extra options for your posts.

How it works:

  1. In ‘Edit Posts’/’Edit Pages,’ you can click on ‘Clone’ link below the post/page title: this will immediately create a copy and return to the list.
  2. NEW! In ‘Edit Posts’/’Edit Pages,’ you can select one or more items, then choose ‘Clone’ in the ‘Bulk Actions’ drop-down to copy them all at once.
  3. In ‘Edit Posts’/’Edit Pages,’ you can click on ‘New Draft’ link below the post/page title.
  4. On the post edit screen, you can click on ‘Copy to a new draft’ above “Cancel”/”Move to trash.”
  5. While viewing a post as a logged in user, you can click on ‘Copy to a new draft’ as a drop down link under “Edit Post” in the admin bar.
How to duplicate page in WordPress using duplicate WordPress page?
How to duplicate page in WordPress using duplicate WordPress page?

Clone: It will duplicate a post and save as new post-draft.

New Draft: It will duplicate the post and let you start editing and change the content.

Duplicate WordPress Page and Post without Plugin

If you are a developer and don’t want to use any plugin, you can do that with a few lines of code. Of course using the plugin is easy, but there are some advantages and disadvantages to using a plugin or not. We will discuss that later.

As a WordPress admin, you access to WordPress code editor. You can find it under Appearance > Editor. Once you are on the editor page, open the theme functions page from the right sidebar.

WordPress theme functions editor page
WordPress theme functions editor page

Note: Please make sure you have a backup of this file before editing. 

Past the following code at the end of functions.php file.

WordPress Post and page duplicator code.

/*
 * This code will add duplicate option for WOrdPress post types
 */

function technig_post_duplicator(){
	global $wpdb;
	if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'technig_post_duplicator' == $_REQUEST['action'] ) ) ) {
		wp_die('No post to duplicate has been supplied!');
	}
 
	/*
	 * Nonce verification for security
	 */
	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_technig_post_duplicator', 'technig_post_duplicator' );
 
/*
 * Add the duplicate link to action list for post_row_actions
 */
function technig_post_duplicator_link( $actions, $post ) {
	if (current_user_can('edit_posts')) {
		$actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=technig_post_duplicator&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
	}
	return $actions;
}
 
add_filter( 'post_row_actions', 'technig_post_duplicator_link', 10, 2 );
add_filter( 'page_row_actions', 'technig_post_duplicator_link', 10, 2 );

Now you have the duplicate option for post and pages.

Use plugins or Code?

If you want to know which one is better, here are some suggestions that weather using code is better or using a plugin.

Using too many plugins in your site slow down your site speed. Especially when your plugin interacts with layouts of your page. For this types of plugin, it doesn’t matter. Since it’s a small feature, you don’t need to use any plugin. Just use the code. Of course, you can choose whatever suits you the best.

If you have any question about how to duplicate page in WordPress, feel free to comment it below. 🙂

 

4 Comments
  1. Majit says

    Hello Hujj, is code same as new plugin without database settings, or different performance?? https://wordpress.org/plugins/duplicate-post-littlebizzy/

  2. Hujjat Nazari says

    Hi,
    The code is not the same, but it does the same actions. maybe the plugin has some more features.

  3. Dennis says

    Awesome tutorial, thank you so much!

    1. Hujjat Nazari says

      You’re very welcome 🙂

Leave A Reply

Your email address will not be published.