Quantcast
Channel: Darkstar Media
Viewing all articles
Browse latest Browse all 48

How to Duplicate a Page in WordPress

$
0
0

If you’re wondering how to duplicate a page in WordPress, we can help. It’s a handy feature for creating templates, testing layout changes, or preserving content structure. I use it often with ACF (Advanced Custom Fields) flexible page blocks, as many of my articles and reviews follow the same format. Just be sure to update all placeholder text accordingly.

Table of Contents

Why Duplicate a Page in WordPress?

Duplicating a page in WordPress is a great way to save time, maintain consistency, and streamline content management. It’s useful for creating templates, testing layouts, and ensuring uniform design across multiple pages. This feature is especially helpful for blogs, product pages, and landing pages, eliminating the need to rebuild layouts from scratch.

It also preserves content structure, including formatting, images, and custom fields, making it ideal for users of ACF, Elementor, or Divi. Whether you’re a site administrator, designer, or content creator, duplicating pages can greatly improve efficiency and workflow.

How to Duplicate a Page in WordPress

1. Using a Plugin

The easiest way to duplicate a page in WordPress is by using a plugin. Two popular plugins include:

  • Yoast Duplicate Post – Best for advanced users with extra customization options.
  • Duplicate Page – Simple and lightweight, with one-click duplication. (My preference).

Duplicate Page:

  1. Go to Plugins > Add New in your WordPress dashboard.
  2. Search for Duplicate Page and install it.
  3. Activate the plugin and go to Pages > All Pages or Posts > All Posts .
  4. Hover over the page/post you want to duplicate and click duplicate this.
  5. Edit the duplicated page as needed and publish it.

2. Manual Duplication

For those who prefer not to use a plugin, manually duplicating a page is an option—though not the most efficient one. This process can be especially challenging with a page builder like Divi, as layouts, custom styles, and dynamic content may not transfer seamlessly. However, if you still want to proceed, follow these steps:

  1. Open the existing page in the WordPress editor.
  2. Switch to Text/HTML mode and copy the content.
  3. Create a New Page under Pages > Add New.
  4. Paste the copied content into the new page.
  5. Adjust formatting, title, and permalinks as needed, then save or publish.

3. Using WordPress Functions

For developers, adding a custom function can enable a duplication feature. Insert this code in your functions.php. This code also handles ACF flexible page blocks and FAQs from the FAQ Schema plugin.


function duplicate_post_as_draft()
{
    if (!isset($_GET['post'])) {
        wp_die('Invalid request');
    }

    $post_id = absint($_GET['post']);

    if (!current_user_can('edit_posts')) {
        wp_die('You do not have permission to duplicate posts.');
    }

    $post = get_post($post_id);
    if (!$post) {
        wp_die('Post not found.');
    }

    // Prepare post data
    $new_post_id = wp_insert_post([
        'post_title'   => $post->post_title . ' Copy',
        'post_content' => $post->post_content,
        'post_status'  => 'draft',
        'post_author'  => get_current_user_id(),
        'post_type'    => $post->post_type,
    ]);

    if ($new_post_id) {
        // Copy metadata, including ACF fields
        $meta_data = get_post_meta($post_id);
        foreach ($meta_data as $key => $values) {
            foreach ($values as $value) {
                // Check for ACF flexible content or repeaters
                if (is_serialized($value)) {
                    $value = unserialize($value);
                }
                add_post_meta($new_post_id, $key, $value);
            }
        }

        // Copy taxonomies (categories, tags)
        $taxonomies = get_object_taxonomies($post->post_type);
        foreach ($taxonomies as $taxonomy) {
            $terms = wp_get_object_terms($post_id, $taxonomy, ['fields' => 'ids']);
            wp_set_object_terms($new_post_id, $terms, $taxonomy);
        }

        // Handle ACF Flexible Content & Repeaters
        if (function_exists('get_field') && function_exists('update_field')) {
            $fields = get_field_objects($post_id);
            if ($fields) {
                foreach ($fields as $field_key => $field) {
                    $value = get_field($field_key, $post_id);

                    // Ensure correct duplication of repeaters & flexible content
                    if ($field['type'] === 'repeater' || $field['type'] === 'flexible_content') {
                        update_field($field_key, $value, $new_post_id);
                    }
                }
            }
        }

        wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
        exit;
    } else {
        wp_die('Error duplicating post.');
    }
}
add_action('admin_action_duplicate_post_as_draft', 'duplicate_post_as_draft');

function add_duplicate_post_link($actions, $post)
{
    if (current_user_can('edit_posts')) {
        $url = admin_url('admin.php?action=duplicate_post_as_draft&post=' . $post->ID);
        $actions['duplicate'] = 'Duplicate';
    }
    return $actions;
}
add_filter('post_row_actions', 'add_duplicate_post_link', 10, 2);
add_filter('page_row_actions', 'add_duplicate_post_link', 10, 2);

Best Practices

Ensure unique permalinks, modify content as necessary, check SEO settings, and test before publishing. I always like to run my pages through Google’s Test your structured data tool.

Conclusion

Duplicating pages in WordPress can streamline your workflow and ensure content consistency. Choose the method that best fits your needs. If you need help, feel free to reach out—we can get you set up quickly and efficiently.


Viewing all articles
Browse latest Browse all 48

Trending Articles