Easily duplicate pages and posts in WordPress
When we work on creating content for our website, we often find the need to use similar structures and designs for different pages and posts. That is where duplicating a page in WordPress becomes an essential tool.
Imagine being able to replicate a page in WordPress with just one click, maintaining all elements and styles. In this article, we will guide you through different methods to achieve it, either with plugins or manually.
Tabla de contenidos
Benefits of duplicating pages in WordPress
Duplicating a page in WordPress is not just a matter of convenience; it offers several benefits such as:
-
- Design consistency: Maintaining a uniform design on pages and posts enhances the user experience.
- Time saving: Avoids the repetitive process of creating pages from scratch.
- Testing: You can test changes on a duplicate page without affecting the live content.
How to duplicate a page in WordPress with plugins
There are several plugins specifically designed to help users clone their WordPress pages.
Using the Duplicate Post plugin
- Install and activate “Duplicate Post”.
- Go to “Posts” or “Pages” and select “Clone” on the entry or page you wish to duplicate.
Duplicating with the Duplicate Page and Post plugin
- Install and activate “Duplicate Page and Post”.
- Simply click on “Duplicate This” on the entry or page you wish to clone.
The Post Duplicator method for cloning pages
- After installing “Post Duplicator”, select “Duplicate Post” or “Duplicate Page” from the dashboard.
Pros and cons of using plugins to duplicate
- Pros: Ease of use, configuration options, and compatibility with most themes.
- Cons: It can increase the site’s load, and not all plugins are updated regularly.
Duplicating a page in WordPress manually
You don’t always need a plugin to duplicate content. Here we show you how to do it manually:
Using Gutenberg
- Open the page you want to duplicate.
- Copy all the content.
- Create a new page and paste the copied content.
With the code editor
- From the text editor, copy all the HTML code of the page.
- Paste this code into a new page or post.
Adding the code in functions.php manually
You can add the functionality to duplicate pages and posts in WordPress manually through the functions.php
file of your active theme. However, it is recommended to do it in a child theme for security and to avoid losing changes with future updates.
Duplicated pages or posts appear in the draft.
function duplicate_content_as_draft() {
global $wpdb;
if (!(isset($_GET['post']) || isset($_POST['post']) || (isset($_REQUEST['action']) && 'duplicate_content_as_draft' == $_REQUEST['action']))) {
wp_die('No content has been selected to duplicate.');
}
$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 . "-copy",
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => 'draft',
'post_title' => $post->post_title . " (Copy)",
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order,
);
$new_post_id = wp_insert_post($args);
$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;
$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('Duplication failed, item not found: ' . $post_id);
}
}
add_action('admin_action_duplicate_content_as_draft', 'duplicate_content_as_draft');
function duplicate_post_link($actions, $post) {
if (current_user_can('edit_posts')) {
$actions['duplicate'] = 'Duplicate';
}
return $actions;
}
add_filter('post_row_actions', 'duplicate_post_link', 10, 2);
Conclusion
Whether it is through plugins or manually, duplicating pages in WordPress is an efficient way to maintain design consistency, save time, and experiment with new content structures without affecting the live site. Choose the method that best suits your skills and needs and start duplicating your pages and posts effortlessly.