WP Turbo Duplicate

Home » Snippets » WP Turbo Duplicate
0

Created with:

Visibility: 

public

Creator: Gusti Maulan

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
/**
 * Plugin Name: Duplicate Page or Post
 * Plugin URI: https://example.com/duplicate-page-post/
 * Description: Allows users to easily duplicate a page or post in WordPress.
 * Version: 1.0.0
 * Author: WP Turbo
 * Author URI: https://wpturbo.dev
 * License: GPLv2 or later
 * Text Domain: wpturbo
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Register the duplicate page/post functionality.
 */
function wpturbo_duplicate_page_post() {
    add_action( 'post_row_actions', 'wpturbo_add_duplicate_link', 10, 2 );
    add_action( 'admin_action_wpturbo_duplicate_page', 'wpturbo_duplicate_page' );
    add_action( 'admin_action_wpturbo_duplicate_post', 'wpturbo_duplicate_post' );
}

/**
 * Add duplicate link to the post row actions.
 *
 * @param array   $actions The post row actions.
 * @param WP_Post $post    The current post object.
 *
 * @return array The modified post row actions.
 */
function wpturbo_add_duplicate_link( $actions, $post ) {
    if ( 'page' === $post->post_type ) {
        $duplicate_url = wp_nonce_url( admin_url( 'admin.php?action=wpturbo_duplicate_page&amp;post=' . $post->ID ), 'wpturbo_duplicate_nonce' );
        $actions['duplicate'] = '<a href="' . $duplicate_url . '" title="' . esc_attr__( 'Duplicate this page', 'wpturbo' ) . '">' . esc_html__( 'Duplicate', 'wpturbo' ) . '</a>';
    } elseif ( 'post' === $post->post_type ) {
        $duplicate_url = wp_nonce_url( admin_url( 'admin.php?action=wpturbo_duplicate_post&amp;post=' . $post->ID ), 'wpturbo_duplicate_nonce' );
        $actions['duplicate'] = '<a href="' . $duplicate_url . '" title="' . esc_attr__( 'Duplicate this post', 'wpturbo' ) . '">' . esc_html__( 'Duplicate', 'wpturbo' ) . '</a>';
    }

    return $actions;
}

/**
 * Duplicate a page.
 */
function wpturbo_duplicate_page() {
    if ( ! isset( $_GET['post'] ) || ! isset( $_GET['action'] ) || 'wpturbo_duplicate_page' !== $_GET['action'] ) {
        return;
    }

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

    if ( ! current_user_can( 'edit_pages' ) || ! wpturbo_verify_nonce() ) {
        wp_die( esc_html__( 'You do not have permission to duplicate this page.', 'wpturbo' ) );
    }

    $new_page_id = wpturbo_duplicate_content( $post_id );

    wp_redirect( admin_url( 'post.php?action=edit&amp;post=' . $new_page_id ) );
    exit;
}

/**
 * Duplicate a post.
 */
function wpturbo_duplicate_post() {
    if ( ! isset( $_GET['post'] ) || ! isset( $_GET['action'] ) || 'wpturbo_duplicate_post' !== $_GET['action'] ) {
        return;
    }

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

    if ( ! current_user_can( 'edit_posts' ) || ! wpturbo_verify_nonce() ) {
        wp_die( esc_html__( 'You do not have permission to duplicate this post.', 'wpturbo' ) );
    }

    $new_post_id = wpturbo_duplicate_content( $post_id );

    wp_redirect( admin_url( 'post.php?action=edit&amp;post=' . $new_post_id ) );
    exit;
}

/**
 * Duplicate the content of a page or post.
 *
 * @param int $post_id The ID of the page or post to duplicate.
 *
 * @return int The ID of the duplicated page or post.
 */
function wpturbo_duplicate_content( $post_id ) {
    $post = get_post( $post_id );

    if ( is_null( $post ) ) {
        return 0;
    }

    $new_post_id = wp_insert_post(
        array(
            'post_title'   => $post->post_title . ' (Duplicate)',
            'post_content' => $post->post_content,
            'post_status'  => 'draft',
            'post_type'    => $post->post_type,
        )
    );

    if ( $new_post_id ) {
        // Duplicate post meta.
        $post_meta = get_post_meta( $post_id );
        foreach ( $post_meta as $meta_key => $meta_values ) {
            foreach ( $meta_values as $meta_value ) {
                add_post_meta( $new_post_id, $meta_key, $meta_value );
            }
        }

        // Duplicate taxonomies.
        $taxonomies = get_object_taxonomies( $post->post_type );
        foreach ( $taxonomies as $taxonomy ) {
            $terms = wp_get_post_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
            wp_set_post_terms( $new_post_id, $terms, $taxonomy, false );
        }

        // Duplicate featured image.
        $thumbnail_id = get_post_thumbnail_id( $post_id );
        if ( $thumbnail_id ) {
            $thumbnail = get_attached_file( $thumbnail_id );
            $thumbnail_data = wp_read_image_metadata( $thumbnail );
            $thumbnail_new = wpturbo_duplicate_featured_image( $thumbnail, $thumbnail_data, $new_post_id );
            if ( $thumbnail_new ) {
                set_post_thumbnail( $new_post_id, $thumbnail_new );
            }
        }
    }

    return $new_post_id;
}

/**
 * Duplicate the featured image of a page or post.
 *
 * @param string $thumbnail The path to the original featured image file.
 * @param array  $metadata  The metadata of the original featured image.
 * @param int    $post_id   The ID of the duplicated page or post.
 *
 * @return int The ID of the duplicated featured image.
 */
function wpturbo_duplicate_featured_image( $thumbnail, $metadata, $post_id ) {
    $filetype = wp_check_filetype( $thumbnail );
    $new_thumbnail = wp_upload_bits( $post_id . '-' . basename( $thumbnail ), null, file_get_contents( $thumbnail ) );

    if ( $new_thumbnail['error'] ) {
        return 0;
    }

    $attachment = array(
        'post_mime_type' => $filetype['type'],
        'post_title'     => $metadata['title'],
        'post_content'   => '',
        'post_status'    => 'inherit',
    );

    $attachment_id = wp_insert_attachment( $attachment, $new_thumbnail['file'], $post_id );
    if ( ! is_wp_error( $attachment_id ) ) {
        wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $new_thumbnail['file'] ) );
        return $attachment_id;
    }

    return 0;
}

/**
 * Verify the duplicate nonce.
 *
 * @return bool Whether the nonce is valid or not.
 */
function wpturbo_verify_nonce() {
    $nonce = isset( $_GET['_wpnonce'] ) ? $_GET['_wpnonce'] : '';
    return wp_verify_nonce( $nonce, 'wpturbo_duplicate_nonce' );
}

// Register the duplicate page/post functionality.
wpturbo_duplicate_page_post();
				

Explanation:

This plugin adds a "Duplicate" link to the row actions of both pages and posts in the WordPress admin area. When the link is clicked, the plugin duplicates the selected page or post, including its content, meta data, taxonomies, and featured image. The duplicated page or post is saved as a draft.

The main function wpturbo_duplicate_page_post() registers the duplicate functionality by hooking into the post_row_actions action and adding the duplicate link to the row actions array.

The wpturbo_add_duplicate_link() function adds the duplicate link to the row actions based on the post type. It generates a unique URL with a nonce for the duplicate action.

The wpturbo_duplicate_page() and wpturbo_duplicate_post() functions handle the duplicate actions for pages and posts respectively. They check for the correct action and verify the nonce before duplicating the content. The duplicated page or post is then redirected to the edit screen.

The wpturbo_duplicate_content() function duplicates the content of a page or post. It uses wp_insert_post() to create a new post with the duplicated content and status set to "draft". It also duplicates the post meta, taxonomies, and featured image.

The wpturbo_duplicate_featured_image() function duplicates the featured image of a page or post. It uploads a new copy of the image file and inserts it as an attachment in the duplicated post.

The wpturbo_verify_nonce() function verifies the duplicate nonce to ensure the action is valid and secure.

Please note that this code snippet assumes that you have already created the plugin file 'wp-plugin.php' and have the necessary plugin header information set.

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