Click here to get this post in PDF
If you are using WordPress to create and manage your website, at some point, you may want to duplicate a page. You might be thinking that you can simply copy and paste all the content onto a new page.
This works only to a certain extent, as a lot of your formatting, such as features, images, and SEO, will all be lost and will need to be redone. For more information on this topic, you can check out this article on duplicating a page in WordPress.
Moreover, if you want to revamp a page and improve it, if you start editing the existing page, instead of making a duplicate, you don’t have any point of comparison. Whatever your reason for needing to duplicate a page in WordPress, there are a couple of ways you can go about it.
Copying a WordPress Page without Plugins
If you don’t want to use a plugin to copy a WordPress page, you can try altering some code.
A Bit of Coding
No worries if you are not a coding guru (very few of us are). You can duplicate WordPress pages by doing a bit of coding, and it’s not that hard. We will be providing you with the code here, so you just have to copy and paste it. Let’s explain how to do this.
Accessing the Functions.php File
To duplicate a page without a plugin, you will need to access the “functions.php” file via the coding method. There are two main methods that you can use to access the functions.php file.
The first method is by using FTP Client, so you first need to install FTP Client. Start the FTP Client, click on “connect,” and provide your username and password to connect to the FTP host server. You can then access the appropriate file by going to “WordPress Root,” then “WP Content,” then “Themes,” then “Current Themes,” and you will finally get to functions.php. You can then download the file, edit it as need be, and upload it back into the appropriate location.
The second method accesses the functions.php file. You can make changes to the code by logging into WordPress as an admin, then go to the appearance editor in the sidebar, select the theme you want to edit, and then select the themes function (functions.php), which is under the “theme files” in the “templates section.” You can now make changes to the code, and when you are done, simply hit the “update” button.
Now you know how to access the functions.php file so you can make changes to the code without a plugin; the primary goal is to duplicate a page. The next step is to enter the appropriate code that will then duplicate the page. Don’t worry if you don’t know much about coding because we have included the necessary code you can just copy and paste.
/*
* Function for post duplication. Dups appear as drafts. The 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(‘page_row_actions’, ‘rd_duplicate_post_link’, 10, 2);
If you just want to duplicate a post, not a page, then replace that last line with:
“add_filter( ‘post_row_actions’, ‘rd_duplicate_post_link’, 10, 2 );”.
Conclusion
You now know how to copy a WordPress page without plugins. As you can see, there are just two steps involved. First, access the functions.php file, copy and paste that code into the file, and then update it. It’s as simple as that, and once you are done, you will have an exact duplicate of your original page.
You may also like: 10 Reasons You Need a Website for Your Business
Image source: Unsplash.com