How to Duplicate WordPress Posts, Pages and Custom Post Types Without A Plugin

As an administrator, editor or even developer, it’s really useful to be able to duplicate WordPress posts, pages and other post types on your website with a single click. Plus, you shouldn’t need to re-enter tags, categories, meta fields and other related options to post every time. 

In this blog post we’re going to add a small code snippet to your website so you will be able to duplicate any post type with a single click. This method works with the JupiterX theme and all other WordPress themes.

Adding a Link Button to Each Post to Duplicate It

To duplicate WordPress posts, first let’s add a link button under each post title so that when it is clicked the post will be duplicated.
Go to the function.php file of your theme and add following code at the end of it:

add_filter( 'post_row_actions', 'jupiterx_add_duplicate_post_button', 10, 2 );
 
function jupiterx_add_duplicate_post_button( $actions, $post ) {
    $args = [
        'jupiterx-to-duplicate' => $post->ID,
        'jupiterx-duplicate-posts' => wp_create_nonce( 'jupiterx-duplicate-nonce' ),
    ];
 
    if ( 'post' !== $post->post_type ) {
        $args['post_type'] = $post->post_type;
    }
 
    $url  = add_query_arg( $args, admin_url( 'edit.php' ) );
 
    $actions['duplicate_post'] = "<a href='{$url}'>Duplicate</a>";
    return $actions;
}

By adding this code, if you navigate to the Edit Post screen you will see the following button under each post:

duplicate WordPress posts - duplicate button

If you click the button now the page will simply reload.

In the next section we are going to add functionality to this button so it will duplicate the related post.

Add Functionality to the Button

After the button is clicked we get post information including tags, categories, meta fields and everything else that belongs to that post. We will then create a new post using this information, completing the process.
Copy and add the following code at the end of your function.php file in your theme, where you added the previous code snippet.

add_action( 'admin_init', 'jupiterx_create_duplicate_button_functionality' );
 
function jupiterx_create_duplicate_button_functionality() {
    if ( ! isset( $_GET['jupiterx-duplicate-posts'] ) ) {
        return;
    }
 
    check_ajax_referer( 'jupiterx-duplicate-nonce', 'jupiterx-duplicate-posts', true );
 
    $post_id = absint( $_GET['jupiterx-to-duplicate'] );
 
    $post = get_post( $post_id );
 
    if ( ! $post ) {
        wp_die( 'We could not find any post to duplicate it.' );
    }
 
    // Create post array exactly like post that was clicked.
    $args = [
        'comment_status' => $post->comment_status,
        'ping_status'    => $post->ping_status,
        'post_author'    => wp_get_current_user()->ID,
        '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 . ' Duplicated',
        'post_type'      => $post->post_type,
        'to_ping'        => $post->to_ping,
        'menu_order'     => $post->menu_order
    ];
 
    $duplicated_post = wp_insert_post( $args );
 
    // Copy taxonomies.
    $taxonomies = get_object_taxonomies( get_post_type( $post ) );
    if( $taxonomies ) {
        foreach ( $taxonomies as $taxonomy ) {
            $post_terms = wp_get_object_terms( $post_id, $taxonomy, [ 'fields' => 'slugs' ] );
            wp_set_object_terms( $duplicated_post, $post_terms, $taxonomy, false );
        }
    }
 
    // Copy meta fields.
    $post_meta = get_post_custom( $post_id );
    if( $post_meta ) {
        foreach ( $post_meta as $meta_key => $meta_values ) {
            update_post_meta( $duplicated_post, $meta_key,  $meta_values[0] );
        }
    }
 
    $args = [];
 
    if ( 'post' !== $post->post_type ) {
        $args['post_type'] = $post->post_type;
    }
 
    $url  = add_query_arg( $args, admin_url( 'edit.php' ) );
 
    wp_safe_redirect( $url );
    exit;
}

Now if you press the duplicate link, the related post will be duplicated and the word “Duplicated” will be added to its title, so you will know which one is the original post.

In following picture you see that the post has been duplicated :

duplicate WordPress posts - duplicated post

Now we have successfully added duplicate post functionality to posts. But what about other post types such as  “page”’, “product” and more?

Let’s tackle this in step 3.

Add Duplication Functionality to any Post Type

To add this functionality to any kind of post type, you need to know the post type slug. For example, the WooCommerce product post type slug is “product”.

After you’ve learned the post type slug, you need to add this line of code to the first code snippet you already added to your function.php file.

To add this functionality to posts we added following code :

add_filter( 'post_row_actions', 'jupiterx_add_duplicate_post_button', 10, 2 );

To add this to pages we just need to add the following line, as we already know that the pages slug is “page”:

add_filter( 'page_row_actions', 'jupiterx_add_duplicate_post_button', 10, 2 );

As you can see, we just replaced “post” with “page”, which means that we just need to add the post type slug at the beginning of the above filter instead of “post” or “page” and the duplication functionality will be added.

Conclusion

Some plugins like Post Duplicator enable you to duplicate WordPress posts on your site, but with just a few lines of code you can add this functionality to your website without using any third party plugin.

If you have any questions about this article, feel free to ask us in the comments. I hope this post has helped you to improve your website’s functionality.

Recommended Posts

17 Comments

  1. Very useful tip, thx mate.

  2. This article has given me information regarding posts and sites that are simple to reach, which has been very beneficial. Yes, knowing this website is fantastic heardle game

  3. The information you have posted is very useful.

  4. I find the information you provide to be both fascinating and instructive, therefore I read it often.

  5. You’ll need to know the slug of the post type to which you want to add this capability. For instance, “product” is the slug for the WooCommerce product post type.

  6. An intriguing discussion might be priced at comment. I do think you should write much more about this topic, it will not often be a taboo subject but generally folks are insufficient to speak on such topics. To another. Cheers

  7. I think this is a fantastic article, and I really appreciate you letting me know about it. That is precisely what I had hoped to find, and I really hope that you will keep sharing content that is of such high quality in the years to come.

  8. يُمكن التخلّص من أبو بريص باستخدام قشر البيض، وذلك عن طريق وضع قشر البيض المكسور في المنطقة التي يظهر فيها أبو بريص، فهو يخاف من قشر البيض، إذّ يعتقد أنّه بيض حيوانات مفترسة أخرى، لذلك سيهرب من المكان بسرعة ولن يتقرب مجددًا من ذلك المكان، وتجدر الإشارة إلى وجوب تغيير قشور البيض مرّة كل 3-4 أسابيع حتى لا تنتشر رائحة غير مستحبّة. استخدام مصيدة الغراء يُمكن استخدام مصيدة الغراء المخصصة للفئران لصيد أبو بريص، إذّ إنّها تعمل بفاعلية لصيد مختلف أنواع الحشرات، مثل: أبو بريص، والصراصير، وغيرها، وعند وضعها يُمكن ملاحظة عدد أبو بريص الذي بإمكانها اصطياده، ويُمكن استخدامها كالآتي: تُستخدم المصيدة بوضعها في زوايا المنزل، إذ يُفضّل أبو بريص الوجود عادةً في الزوايا. تُترك المصيدة ليمر عليها أبو بريص، ويلتصق بها. يُتخلّص من أبو بريص بعد اصطياده، لأن المصيدة تؤدي إلى موت أبو بريص

    شركة مكافحة حشرات بالمدينة المنورة

  9. I was simply searching the internet for some information when I came across your article. This website’s content has really impressed me. It exhibits your understanding of the topic.

  10. Do you enjoy playing video games? fireboy and watergirl: The Challenge is a fun and intelligent 2-player game that improves thinking, problem-solving skills, cooperation, and friendship.

  11. I get ideas from people who make new things. How great it is to see something new being made! quordle

  12. This is very important as to make sure that every possible interference is connected just like the
    Amapiano

  13. Stream And download to “” Fakaza Mp3 320kbps tubidy mobi music mp3 Juice wapdam Hiphopza flexyjams mp3lio Zippyshare Fakaza Mp3 Download download datafilehost torrent soundcloud to mp3 audio.

    Stream and Download

    Download Audio

  14. You may come across plenty of Music Streaming Service on the web, but nothing can match the experience you get at Fakaza. It is home to a wide selection of songs from top artists across the globe. Whether you are looking for hip-hop, gospel, or rock, we have you covered. Every time you land on our website, you will be treated with the latest Latest Songs Mp3 Download  per your liking. Our attractive user interface makes it easy for you to search and listen to your favorite tracks quickly. Mp3 Latest Songs 


Add a Comment

Your email address will not be published. Required fields are marked *