/** * Create a duplicate from a post */ function duplicate_post_create_duplicate($post, $status = '', $parent_id = '') { // We don't want to clone revisions if ($post->post_type == 'revision') { return; } if ($post->post_type != 'attachment') { $prefix = get_option('duplicate_post_title_prefix'); $suffix = get_option('duplicate_post_title_suffix'); if (!empty($prefix)) { $prefix .= " "; } if (!empty($suffix)) { $suffix = " " . $suffix; } if (get_option('duplicate_post_copystatus') == 0) { $status = 'draft'; } } $new_post_author = duplicate_post_get_current_user(); $new_post = array('menu_order' => $post->menu_order, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_author' => $new_post_author->ID, 'post_content' => $post->post_content, 'post_excerpt' => get_option('duplicate_post_copyexcerpt') == '1' ? $post->post_excerpt : "", 'post_mime_type' => $post->post_mime_type, 'post_parent' => $new_post_parent = empty($parent_id) ? $post->post_parent : $parent_id, 'post_password' => $post->post_password, 'post_status' => $new_post_status = empty($status) ? $post->post_status : $status, 'post_title' => $prefix . $post->post_title . $suffix, 'post_type' => $post->post_type); if (get_option('duplicate_post_copydate') == 1) { $new_post['post_date'] = $new_post_date = $post->post_date; $new_post['post_date_gmt'] = get_gmt_from_date($new_post_date); } $new_post_id = wp_insert_post($new_post); // If you have written a plugin which uses non-WP database tables to save // information about a post you can hook this action to dupe that data. if ($post->post_type == 'page' || function_exists('is_post_type_hierarchical') && is_post_type_hierarchical($post->post_type)) { do_action('dp_duplicate_page', $new_post_id, $post); } else { do_action('dp_duplicate_post', $new_post_id, $post); } delete_post_meta($new_post_id, '_dp_original'); add_post_meta($new_post_id, '_dp_original', $post->ID); // If the copy is published or scheduled, we have to set a proper slug. if ($new_post_status == 'publish' || $new_post_status == 'future') { $post_name = wp_unique_post_slug($post->post_name, $new_post_id, $new_post_status, $post->post_type, $new_post_parent); $new_post = array(); $new_post['ID'] = $new_post_id; $new_post['post_name'] = $post_name; // Update the post into the database wp_update_post($new_post); } return $new_post_id; }
/** * Create a duplicate from a page */ function duplicate_post_create_duplicate_from_page($post) { global $wpdb; //$new_post_type = 'page'; $new_post_author = duplicate_post_get_current_user(); $new_post_date = get_option('duplicate_post_copydate') == 1 ? $post->post_date : current_time('mysql'); $new_post_date_gmt = get_gmt_from_date($new_post_date); $prefix = get_option('duplicate_post_title_prefix'); if (!empty($prefix)) { $prefix .= " "; } $new_post_type = $post->post_type; $post_content = str_replace("'", "''", $post->post_content); $post_content_filtered = str_replace("'", "''", $post->post_content_filtered); $post_excerpt = str_replace("'", "''", $post->post_excerpt); $post_title = $prefix . str_replace("'", "''", $post->post_title); $post_status = str_replace("'", "''", $post->post_status); $post_name = str_replace("'", "''", $post->post_name); $comment_status = str_replace("'", "''", $post->comment_status); $ping_status = str_replace("'", "''", $post->ping_status); // Insert the new template in the post table $wpdb->query("INSERT INTO {$wpdb->posts}\n\t\t\t(post_author, post_date, post_date_gmt, post_content, post_content_filtered, post_title, post_excerpt, post_status, post_type, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_parent, menu_order, post_mime_type)\n\t\t\tVALUES\n\t\t\t('{$new_post_author->ID}', '{$new_post_date}', '{$new_post_date_gmt}', '{$post_content}', '{$post_content_filtered}', '{$post_title}', '{$post_excerpt}', 'draft', '{$new_post_type}', '{$comment_status}', '{$ping_status}', '{$post->post_password}', '{$post_name}', '{$post->to_ping}', '{$post->pinged}', '{$new_post_date}', '{$new_post_date_gmt}', '{$post->post_parent}', '{$post->menu_order}', '{$post->post_mime_type}')"); $new_page_id = $wpdb->insert_id; // Copy the taxonomies duplicate_post_copy_post_taxonomies($post->ID, $new_page_id, $post->post_type); // Copy the meta information duplicate_post_copy_post_meta_info($post->ID, $new_page_id); return $new_page_id; }