Duplica post e pagine

Duplica post, pagine o custom post type con 1 clic.
PHP
function duplicate_post_as_draft() {
    global $wpdb;
 
    // Check if the 'post' parameter is set in either GET or POST request
    if (!isset($_GET["post"]) && !isset($_POST["post"])) {
        wp_die("Non è stato fornito alcun post da duplicare!");
    }
 
    // Verify the nonce for security
    if (!isset($_GET["duplicate_nonce"]) || !wp_verify_nonce($_GET["duplicate_nonce"], basename(__FILE__))) {
        return;
    }
 
    // Get the post ID and ensure it's an integer
    $post_id = isset($_GET["post"]) ? absint($_GET["post"]) : absint($_POST["post"]);
 
    // Check if the post exists and is not null
    $post = get_post($post_id);
    if (!$post) {
        wp_die("Creazione del post fallita, impossibile trovare il post originale: " . esc_html($post_id));
    }
 
    $current_user = wp_get_current_user();
    $new_post_author = $current_user->ID;
 
    $args = [
        "comment_status" => $post->comment_status,
        "ping_status" => $post->ping_status,
        "post_author" => $new_post_author,
        "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 . " (Copia)",
        "post_type" => $post->post_type,
        "to_ping" => $post->to_ping,
        "menu_order" => $post->menu_order
    ];
 
    $new_post_id = wp_insert_post($args);
 
    $taxonomies = get_object_taxonomies($post->post_type);
    foreach ($taxonomies as $taxonomy) {
        $post_terms = wp_get_object_terms($post_id, $taxonomy, ["fields" => "slugs"]);
        wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
    }
 
    $post_meta_infos = $wpdb->get_results(
        $wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id = %d", $post_id)
    );
    if (count($post_meta_infos) != 0) {
        foreach ($post_meta_infos as $meta_info) {
            $meta_key = $meta_info->meta_key;
            $meta_value = sanitize_meta($meta_info->meta_key, $meta_info->meta_value, "post");
            $wpdb->insert($wpdb->postmeta, [
                "post_id" => $new_post_id,
                "meta_key" => $meta_key,
                "meta_value" => $meta_value
            ]);
        }
    }
 
    // Redirect to the post list screen and show a success message
    $redirect_url = admin_url("edit.php?post_type=" . $post->post_type);
    wp_redirect(add_query_arg("message", "101", $redirect_url));
    exit();
}
 
add_action("admin_action_duplicate_post_as_draft", "duplicate_post_as_draft");
 
function duplicate_post_link($actions, $post) {
    if (current_user_can("edit_posts")) {
        $actions["duplicate"] = '<a href="' .
            wp_nonce_url("admin.php?action=duplicate_post_as_draft&post=" . $post->ID, basename(__FILE__), "duplicate_nonce") .
            '" title="Duplica questo articolo" rel="permalink">Duplica</a>';
    }
    return $actions;
}
 
add_filter("post_row_actions", "duplicate_post_link", 10, 2);
add_filter("page_row_actions", "duplicate_post_link", 10, 2);
 
function apply_duplicate_post_link_to_cpts() {
    $post_types = get_post_types(["public" => true], "names");
    foreach ($post_types as $post_type) {
        add_filter("{$post_type}_row_actions", "duplicate_post_link", 10, 2);
    }
}
 
add_action("admin_init", "apply_duplicate_post_link_to_cpts");
 
function show_duplicate_admin_notice() {
    if (isset($_GET['message']) && $_GET['message'] === '101') {
        echo '<div class="notice notice-success is-dismissible"><p>' . esc_html('Post duplicato con successo.') . '</p></div>';
    }
}
 
add_action('admin_notices', 'show_duplicate_admin_notice');

Copia il codice nel file functions.php del tuo tema child attivo o utilizza il plugin Code Snippets.

Lascia un commento

DMM Web Design
Questo sito utilizza cookie tecnici e di profilazione. Cliccando su accetta si autorizzano tutti i cookie di profilazione. Cliccando su rifiuta o la X si rifiutano tutti i cookie di profilazione. Cliccando su personalizza è possibile selezionare quali cookie di profilazione attivare.