Duplicate a page or post without a plugin
Duplicate a Page or Post Without a Plugin in WordPress
Need to duplicate a page or post in WordPress but want to avoid installing another plugin? This quick guide shows you how to clone any page or post manually by adding a simple code snippet to your theme’s functions.php
file. It’s a clean, lightweight solution that keeps your site fast and clutter-free.
Quick Fix: Duplicate a Page or Post Without a Plugin
- Access your WordPress theme’s
functions.php
file via FTP or the theme editor. - Copy and paste the provided duplication function code into
functions.php
. - Save the file and refresh your WordPress admin dashboard.
- Go to the Pages or Posts list, hover over the item you want to duplicate, and click the new “Duplicate” link.
- A draft copy of the page or post will be created immediately, ready for editing.
Why This Happens
WordPress does not include a built-in “duplicate” or “clone” feature for pages or posts. Many users rely on plugins to add this functionality, but plugins can add extra overhead and potential security risks. By adding a custom function, you can create a native duplication option that integrates seamlessly with your admin interface without relying on third-party code.
Requirements
- Basic familiarity with editing WordPress theme files.
- Access to your site’s
functions.php
file via FTP, SFTP, or the WordPress theme editor. - Administrator privileges in WordPress.
- Backup your site before making any code changes to avoid accidental data loss.
Step-by-Step: Add Duplicate Page/Post Functionality
- Backup your site. Always create a full backup before editing theme files.
- Open your theme’s
functions.php
file. You can do this via FTP or from the WordPress dashboard under Appearance > Theme Editor. - Paste the following code at the end of the
functions.php
file:
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!');
}
// Get the original post id
$post_id = (isset($_GET['post']) ? absint($_GET['post']) : absint($_POST['post']));
// Get the original post
$post = get_post($post_id);
// Check if post exists
if (null == $post) {
wp_die('Post does not exist!');
}
// Check user permissions
if (!current_user_can('edit_posts')) {
wp_die('You do not have sufficient permissions to duplicate this post.');
}
// New post data array
$new_post = array(
'post_title' =$post-post_title . ' (Copy)',
'post_content' =$post-post_content,
'post_status' ='draft',
'post_type' =$post-post_type,
'post_author' =wp_get_current_user()-ID,
'post_excerpt' =$post-post_excerpt,
'post_parent' =$post-post_parent,
'menu_order' =$post-menu_order,
'comment_status' =$post-comment_status,
'ping_status' =$post-ping_status,
'post_password' =$post-post_password,
'to_ping' =$post-to_ping,
'pinged' =$post-pinged,
'post_content_filtered' =$post-post_content_filtered,
'post_mime_type' =$post-post_mime_type,
'guid' ='',
);
// Insert the post by wp_insert_post()
$new_post_id = wp_insert_post($new_post);
// Copy taxonomies
$taxonomies = get_object_taxonomies($post-post_type);
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);
}
// Copy post meta
$post_meta = get_post_meta($post_id);
foreach ($post_meta as $meta_key =$meta_values) {
foreach ($meta_values as $meta_value) {
add_post_meta($new_post_id, $meta_key, maybe_unserialize($meta_value));
}
}
// Redirect to the edit post screen for the new draft
wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
exit;
}
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'] = '';
}
return $actions;
}
add_filter('post_row_actions', 'rd_duplicate_post_link', 10, 2);
add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);
- Save the
functions.php
file. - Go to the Pages or Posts list in your WordPress admin. Hover over any item and click the new Duplicate link.
- Edit the duplicated draft. The copy will open in the editor as a draft, ready for your changes.
Common Pitfalls
- Editing the wrong
functions.php
file: Make sure you edit the active theme’sfunctions.php
. Child themes are recommended to avoid losing changes on updates. - Missing user permissions: The duplication function checks if the user can edit posts. If you don’t have sufficient rights, the link won’t appear or duplication will fail.
- Meta data serialization: The code uses
maybe_unserialize()
to handle serialized meta values correctly. Avoid modifying this unless you understand serialization. - Taxonomies not copied: The code copies all taxonomies assigned to the original post, but custom taxonomies may require additional handling if they have complex relationships.