Add Snippet To 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>',
esc_url( admin_url( 'admin-ajax.php?action=wpturbo_copy_post&post_id=' . $post->ID . '&nonce=' . $copy_nonce ) ),
$post->ID,
$copy_nonce,
esc_html__( '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
if ( ! wp_verify_nonce( $_GET['nonce'], 'wpturbo_copy_post_nonce' ) ) {
wp_die( esc_html__( 'Invalid nonce.', 'wpturbo' ) );
}
// 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 and perform AJAX actions
if ( ! current_user_can( 'edit_post', $post_id ) || ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have permission to copy this post.', 'wpturbo' ) );
}
// Get the post object
$post = get_post( $post_id );
// Get the featured image ID
$featured_image_id = get_post_thumbnail_id( $post_id );
// Get the excerpt
$excerpt = $post->post_excerpt;
// Create a new post with the duplicate content, featured image, and excerpt
$new_post_id = wp_insert_post( array(
'post_title' => sanitize_text_field( $post->post_title ),
'post_content' => wp_kses_post( $post->post_content ),
'post_type' => $post->post_type,
'post_status' => 'draft',
'post_author' => get_current_user_id(),
'post_excerpt' => $excerpt,
) );
// Copy the featured image to the new post
if ( ! is_wp_error( $new_post_id ) && $featured_image_id ) {
set_post_thumbnail( $new_post_id, $featured_image_id );
}
// Copy all fields and custom fields from the original post to the new post
if ( ! is_wp_error( $new_post_id ) ) {
// Get all fields and custom fields from the original post
$fields = get_fields( $post_id );
// Update the new post with the fields and custom fields
if ( $fields ) {
foreach ( $fields as $key => $value ) {
update_field( $key, $value, $new_post_id );
}
}
// Copy the 'guides' custom field group
if ( function_exists( 'acf_copy_field_group' ) ) {
acf_copy_field_group( 'guides', $new_post_id );
}
// Copy the 'programs' custom field group
if ( function_exists( 'acf_copy_field_group' ) ) {
acf_copy_field_group( 'programs', $new_post_id );
}
// Redirect the user to the edit screen of the new post
wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
exit;
} else {
wp_die( esc_html__( 'Failed to copy the post.', 'wpturbo' ) );
}
}
Explanation:
In this updated code, we have added the functionality to copy the custom field groups 'guides' and 'programs' along with the post.
After creating the new post with the duplicate content, we use the acf_copy_field_group() function to copy the 'guides' and 'programs' custom field groups to the new post. The acf_copy_field_group() function is provided by the Advanced Custom Fields (ACF) plugin and allows you to copy a custom field group from one post to another.
This ensures that the 'guides' and 'programs' custom field groups are copied over to the new post, allowing you to preserve their fields and settings when duplicating a post.
Remember to save the file and update the plugin in the WordPress admin area. After updating, when you click the 'Copy Post' link, it will duplicate the post along with the featured image, excerpt, 'guides' custom field group, and 'programs' custom field group, and redirect you to the edit screen of the new post.