Create a plugin that adds a copy post link next to the quick edit link. When the copy post link is clicked it should create a new post with the duplicate content.

Home » Snippets » Create a plugin that adds a copy post link next to the quick edit link. When the copy post link is clicked it should create a new post with the duplicate content.
0

Created with:

Visibility: 

public

Creator: Jorge

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: WP Turbo Copy Post
 * Plugin URI: https://wpturbo.dev
 * Description: Adds a copy post link next to the quick edit link to duplicate posts.
 * Version: 1.0.0
 * Author: WP Turbo
 * Author URI: https://wpturbo.dev
 * Text Domain: wpturbo
 */

// Add the Copy Post link next to the Quick Edit link in the Actions column
add_filter( 'post_row_actions', 'wpturbo_add_copy_post_link', 10, 2 );
function wpturbo_add_copy_post_link( $actions, $post ) {
    // Check if the user has the capability to edit the post
    if ( current_user_can( 'edit_post', $post->ID ) ) {
        // Create a nonce for security purposes
        $copy_nonce = wp_create_nonce( 'wpturbo_copy_post_nonce' );
        
        // Add the Copy Post link to the actions array
        $actions['copy'] = sprintf(
            '<a href="%1$s" class="wpturbo-copy-post-link" data-post-id="%2$d" data-nonce="%3$s">%4$s</a>',
            admin_url( 'admin-ajax.php?action=wpturbo_copy_post&post_id=' . $post->ID . '&nonce=' . $copy_nonce ),
            $post->ID,
            $copy_nonce,
            __( 'Copy Post', 'wpturbo' )
        );
    }
    
    return $actions;
}

// Handle the AJAX request to copy the post
add_action( 'wp_ajax_wpturbo_copy_post', 'wpturbo_copy_post' );
function wpturbo_copy_post() {
    // Verify the AJAX request
    check_ajax_referer( 'wpturbo_copy_post_nonce', 'nonce' );
    
    // Get the post ID from the AJAX request
    $post_id = isset( $_GET['post_id'] ) ? intval( $_GET['post_id'] ) : 0;
    
    // Make sure the user has the capability to edit the post
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        wp_die( __( 'You do not have permission to copy this post.', 'wpturbo' ) );
    }
    
    // Get the post object
    $post = get_post( $post_id );
    
    // Create a new post with the duplicate content
    $new_post_id = wp_insert_post( array(
        'post_title'   => $post->post_title,
        'post_content' => $post->post_content,
        'post_type'    => $post->post_type,
        'post_status'  => 'draft',
        'post_author'  => get_current_user_id(),
    ) );
    
    // Redirect the user to the edit screen of the new post
    if ( ! is_wp_error( $new_post_id ) ) {
        wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
        exit;
    } else {
        wp_die( __( 'Failed to copy the post.', 'wpturbo' ) );
    }
}
				

Explanation:

This plugin adds a 'Copy Post' link next to the 'Quick Edit' link in the actions column of the posts list table. When the 'Copy Post' link is clicked, an AJAX request is triggered to duplicate the post. The duplicated post is created as a draft with the same title and content as the original post. The user is then redirected to the edit screen of the new post.

To achieve this, we first add the 'Copy Post' link using the 'post_row_actions' filter. Inside the filter callback function, we check if the current user has the capability to edit the post and create a nonce for security purposes. We then add the 'Copy Post' link to the actions array, passing the necessary data attributes and localized text.

Next, we handle the AJAX request using the 'wp_ajax_wpturbo_copy_post' action. Inside the AJAX callback function, we verify the nonce and check if the user has the capability to edit the post. We then retrieve the post ID from the AJAX request and use it to get the post object. We create a new post with the duplicate content by calling the 'wp_insert_post' function with the necessary parameters. If the new post is successfully created, we redirect the user to the edit screen of the new post. If there is an error, we display an error message.

Remember to save the file and activate the plugin in the WordPress admin area. After activation, you should see the 'Copy Post' link next to the 'Quick Edit' link in the posts list table. Clicking the 'Copy Post' link will duplicate the post and redirect you to the edit screen of the new post.

Register an account to save your snippets or go Pro to get more features.