Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Tag: Copy

Duplicate a page or post without a plugin

Posted on August 19, 2025 By Admin No Comments on 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

  1. Access your WordPress theme’s functions.php file via FTP or the theme editor.
  2. Copy and paste the provided duplication function code into functions.php.
  3. Save the file and refresh your WordPress admin dashboard.
  4. Go to the Pages or Posts list, hover over the item you want to duplicate, and click the new “Duplicate” link.
  5. 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

  1. Backup your site. Always create a full backup before editing theme files.
  2. Open your theme’s functions.php file. You can do this via FTP or from the WordPress dashboard under Appearance > Theme Editor.
  3. 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);
  1. Save the functions.php file.
  2. Go to the Pages or Posts list in your WordPress admin. Hover over any item and click the new Duplicate link.
  3. 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’s functions.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.

…
Admin & Blocks

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com