# Rewrite and Republish Published Posts Safely

> Adds a "Rewrite & republish" link to published posts that creates a private working copy; when the copy is published, its content replaces the original at the same URL and the copy is removed.

- Type: PHP
- Runs: Run everywhere
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: duplicate, editorial, publishing
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/content-rewrite-and-republish/

## Code

```php
add_filter( 'post_row_actions', function ( $actions, $post ) {
    if ( 'publish' === $post->post_status && 'post' === $post->post_type && current_user_can( 'edit_post', $post->ID ) ) {
        $actions['wpide_rewrite'] = sprintf(
            '<a href="%s">Rewrite &amp; republish</a>',
            esc_url( wp_nonce_url( admin_url( 'admin-post.php?action=wpide_rewrite&post=' . $post->ID ), 'wpide_rewrite_' . $post->ID ) )
        );
    }
    return $actions;
}, 10, 2 );

add_action( 'admin_post_wpide_rewrite', function () {
    $post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0;
    check_admin_referer( 'wpide_rewrite_' . $post_id );
    $post = get_post( $post_id );
    if ( ! $post || ! current_user_can( 'edit_post', $post_id ) ) {
        wp_die( 'Not allowed.', 403 );
    }

    $copy_id = wp_insert_post( wp_slash( array(
        'post_type'     => $post->post_type,
        'post_status'   => 'draft',
        'post_title'    => $post->post_title,
        'post_content'  => $post->post_content,
        'post_excerpt'  => $post->post_excerpt,
        'post_author'   => get_current_user_id(),
        'post_category' => wp_get_post_categories( $post_id ),
        'tags_input'    => wp_get_post_tags( $post_id, array( 'fields' => 'names' ) ),
    ) ) );
    update_post_meta( $copy_id, '_wpide_rewrite_of', $post_id );
    if ( has_post_thumbnail( $post_id ) ) {
        set_post_thumbnail( $copy_id, get_post_thumbnail_id( $post_id ) );
    }

    wp_safe_redirect( get_edit_post_link( $copy_id, 'url' ) );
    exit;
} );

add_action( 'transition_post_status', function ( $new_status, $old_status, $copy ) {
    if ( 'publish' !== $new_status || 'publish' === $old_status ) {
        return;
    }
    $original_id = (int) get_post_meta( $copy->ID, '_wpide_rewrite_of', true );
    if ( ! $original_id || ! get_post( $original_id ) ) {
        return;
    }

    // Copy the new version onto the original, keeping its URL and date.
    wp_update_post( wp_slash( array(
        'ID'           => $original_id,
        'post_title'   => $copy->post_title,
        'post_content' => $copy->post_content,
        'post_excerpt' => $copy->post_excerpt,
    ) ) );
    wp_set_post_categories( $original_id, wp_get_post_categories( $copy->ID ) );
    wp_set_post_tags( $original_id, wp_get_post_tags( $copy->ID, array( 'fields' => 'names' ) ) );
    if ( has_post_thumbnail( $copy->ID ) ) {
        set_post_thumbnail( $original_id, get_post_thumbnail_id( $copy->ID ) );
    }

    delete_post_meta( $copy->ID, '_wpide_rewrite_of' );
    wp_trash_post( $copy->ID );
}, 10, 3 );

add_action( 'admin_notices', function () {
    $post = get_post();
    $screen = get_current_screen();
    if ( $post && $screen && 'post' === $screen->base && ( $original = (int) get_post_meta( $post->ID, '_wpide_rewrite_of', true ) ) ) {
        printf( '<div class="notice notice-info"><p>This is a working copy of <a href="%s">%s</a>. Publishing it will update the original.</p></div>', esc_url( get_permalink( $original ) ), esc_html( get_the_title( $original ) ) );
    }
} );
```

## Notes

The original stays live and unchanged until you publish the copy; comments, URL and publish date of the original are kept.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Rewrite and Republish Published Posts Safely" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
