Why Duplicating Posts and Pages Matters
Before we dive into the nitty-gritty of duplicating without plugins, let's understand why this process holds such significance in website management. Duplicating posts and pages is a fundamental requirement in various scenarios, including:
1. Creating Templates for Consistency
In the fast-paced world of digital content, maintaining consistency is crucial. When you have a template that works perfectly for your blog posts or landing pages, duplicating it can save you valuable time. You can reuse the same structure, formatting, and design while only needing to modify the content to fit your new topic.
2. Enhancing User Experience
By duplicating popular and well-structured posts, you can create new, similar content that resonates with your audience. This way, you cater to their interests and preferences while providing fresh information and insights.
3. Time-Saving Content Updates
Suppose you have a post or page that serves as a standard reference for your industry. Instead of recreating the entire content from scratch when updates are needed, you can duplicate the original post and make the necessary changes, saving you a considerable amount of time.
A Step-by-Step Guide: How to do Duplicate Posts and Pages without Plugins
-
Go to your WordPress dashboard and navigate to "Appearance" -> "Theme Editor."
-
In the Theme Editor, locate the "functions.php" file on the right-hand side.
-
Add the following code snippet at the end of the functions.php file:
function duplicate_page_post() {
global $wpdb;
if (isset($_GET['post']) && isset($_GET['action']) && $_GET['action'] == 'duplicate') {
$post_id = absint($_GET['post']);
$post = get_post($post_id);
if (isset($post) && $post != null) {
$new_post_author = $post->post_author;
$current_time = current_time('mysql');
$post_title = $post->post_title . ' (Duplicate)';
$post_content = $post->post_content;
$post_status = 'draft';
$post_type = $post->post_type;
$post_parent = $post->post_parent;
$post_excerpt = $post->post_excerpt;
$post_date = $post->post_date;
$post_date_gmt = $post->post_date_gmt;
$comment_status = $post->comment_status;
$ping_status = $post->ping_status;
$post_password = $post->post_password;
$post_name = $post->post_name;
$to_ping = $post->to_ping;
$pinged = $post->pinged;
$post_modified = current_time('mysql');
$post_modified_gmt = current_time('mysql', 1);
$post_content_filtered = $post->post_content_filtered;
$post_parent = $post->post_parent;
$guid = get_home_url() . '/' . $post->post_name;
$menu_order = $post->menu_order;
$post_mime_type = $post->post_mime_type;
$sql = $wpdb->prepare("INSERT INTO $wpdb->posts
(post_author, post_date, post_date_gmt, post_content, post_title, post_excerpt, post_status, comment_status, ping_status, post_password, post_name, to_ping, pinged, post_modified, post_modified_gmt, post_content_filtered, post_parent, guid, menu_order, post_type, post_mime_type)
VALUES
(%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %s, %d, %s, %s)",
$new_post_author, $post_date, $post_date_gmt, $post_content, $post_title, $post_excerpt, $post_status, $comment_status, $ping_status, $post_password, $post_name, $to_ping, $pinged, $post_modified, $post_modified_gmt, $post_content_filtered, $post_parent, $guid, $menu_order, $post_type, $post_mime_type);
$wpdb->query($sql);
$new_post_id = $wpdb->insert_id;
$taxonomies = get_object_taxonomies($post_type);
foreach ($taxonomies as $taxonomy) {
$post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
}
$post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
if (count($post_meta_infos) != 0) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
foreach ($post_meta_infos as $meta_info) {
$meta_key = $meta_info->meta_key;
$meta_value = addslashes($meta_info->meta_value);
$sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
}
$sql_query.= implode(" UNION ALL ", $sql_query_sel);
$wpdb->query($sql_query);
}
wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
exit;
}
}
}
add_action('admin_action_duplicate', 'duplicate_page_post');
function add_duplicate_link_row($actions, $post) {
if (current_user_can('edit_posts')) {
$actions['duplicate'] = 'Duplicate';
}
return $actions;
}
add_filter('page_row_actions', 'add_duplicate_link_row', 10, 2);
add_filter('post_row_actions', 'add_duplicate_link_row', 10, 2);