On this brief publish I’m going to point out you the way to duplicate a WordPress web page or publish with a single click on. Not solely is the strategy actually easy, however it can copy all of the meta date your publish might have (customized fields, featured picture and many others.)
Video: The way to Duplicate a WordPress Web page or Publish and not using a plugin?
The place so as to add the code?
Add a snippet proven right here under to your youngster theme’s features.php file or higher but, use a snippet supervisor like Code Snippets or WpCodeBox (my favourite).
WpCodeBox is my favourite code snippets supervisor for WordPress. It is a premium plugin and in the event you’re , then seize WPCodeBox with a pleasant 20% low cost right here (SAVE 20% Coupon WPSH20).
The way to Duplicate a WordPress Web page or Publish with a single click on?
Subsequent, simply seize the code right here under and it’ll add a “Duplicate” hyperlink inside your fast hyperlinks. See the screenshot.
For those who click on this hyperlink then:
- Duplicate of the publish will probably be created
- Standing of the publish is “Draft”
- You can be redirected to the publish modifying web page
So, with a view to make it work, simply use this code right here under.
add_filter("post_row_actions", "wpsh_add_duplicate_link", 10, 2);
add_filter("page_row_actions", "wpsh_add_duplicate_link", 10, 2); // add the hyperlink to pages too
operate wpsh_duplicate_post_as_draft()
{
if (!current_user_can("edit_posts")) {
return;
}
if (
!isset($_GET["duplicate_nonce"]) ||
!wp_verify_nonce($_GET["duplicate_nonce"], basename(__FILE__))
) {
return;
}
world $wpdb;
if (
!(
isset($_GET["post"]) ||
isset($_POST["post"]) ||
(isset($_REQUEST["action"]) &&
"wpsh_duplicate_post_as_draft" == $_REQUEST["action"])
)
) {
wp_die("No publish to duplicate has been provided!");
}
// This on right here will get the unique publish id and publish all the unique publish knowledge
$post_id = isset($_GET["post"])
? absint($_GET["post"])
: absint($_POST["post"]);
$publish = get_post($post_id);
// In case you don't need present consumer to be the brand new publish creator, then change this line to this: $new_post_author = $post->post_author;
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
// If publish knowledge exists, create the publish duplicate
if (isset($publish) && $publish != null) {
$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,
"post_type" => $post->post_type,
"to_ping" => $post->to_ping,
"menu_order" => $post->menu_order,
];
$new_post_id = wp_insert_post($args);
// Get all present publish phrases advert set them to the brand new publish draft
$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);
}
// Duplicate all publish meta
$post_meta_infos = $wpdb->get_results(
"SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"
);
if (depend($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;
if ($meta_key == "_wp_old_slug") {
proceed;
}
$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->question($sql_query);
}
// Redirect to the edit publish display screen for the brand new draft
wp_safe_redirect(
admin_url("publish.php?motion=edit&publish=" . $new_post_id)
);
exit();
} else {
wp_die(
"Publish creation failed, couldn’t discover authentic publish: " . $post_id
);
}
}
add_action(
"admin_action_wpsh_duplicate_post_as_draft",
"wpsh_duplicate_post_as_draft"
);
// Add the "Duplicate" hyperlink to motion record for post_row_actions
operate wpsh_add_duplicate_link($actions, $publish)
{
if (current_user_can("edit_posts")) {
$actions["duplicate"] =
'<a href="' .
wp_nonce_url(
"admin.php?motion=wpsh_duplicate_post_as_draft&publish=" . $post->ID,
basename(__FILE__),
"duplicate_nonce"
) .
'" title="Duplicate this merchandise" rel="permalink">Duplicate
// Duplicate a WordPress Web page or Publish with a Single Click on
add_filter("post_row_actions", "wpsh_add_duplicate_link", 10, 2);
add_filter("page_row_actions", "wpsh_add_duplicate_link", 10, 2); // add the hyperlink to pages too
operate wpsh_duplicate_post_as_draft()
{
if (!current_user_can("edit_posts")) {
return;
}
if (
!isset($_GET["duplicate_nonce"]) ||
!wp_verify_nonce($_GET["duplicate_nonce"], basename(__FILE__))
) {
return;
}
world $wpdb;
if (
!(
isset($_GET["post"]) ||
isset($_POST["post"]) ||
(isset($_REQUEST["action"]) &&
"wpsh_duplicate_post_as_draft" == $_REQUEST["action"])
)
) {
wp_die("No publish to duplicate has been provided!");
}
// This on right here will get the unique publish id and publish all the unique publish knowledge
$post_id = isset($_GET["post"])
? absint($_GET["post"])
: absint($_POST["post"]);
$publish = get_post($post_id);
// In case you don't need present consumer to be the brand new publish creator, then change this line to this: $new_post_author = $post->post_author;
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
// If publish knowledge exists, create the publish duplicate
if (isset($publish) && $publish != null) {
$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,
"post_type" => $post->post_type,
"to_ping" => $post->to_ping,
"menu_order" => $post->menu_order,
];
$new_post_id = wp_insert_post($args);
// Get all present publish phrases advert set them to the brand new publish draft
$taxonomies = get_object_taxonomies($publish->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);
}
// Duplicate all publish meta
$post_meta_infos = $wpdb->get_results(
"SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"
);
if (depend($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;
if ($meta_key == "_wp_old_slug") {
proceed;
}
$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->question($sql_query);
}
// Redirect to the edit publish display screen for the brand new draft
wp_safe_redirect(
admin_url("publish.php?motion=edit&publish=" . $new_post_id)
);
exit();
} else {
wp_die(
"Publish creation failed, couldn't discover authentic publish: " . $post_id
);
}
}
add_action(
"admin_action_wpsh_duplicate_post_as_draft",
"wpsh_duplicate_post_as_draft"
);
// Add the "Duplicate" hyperlink to motion record for post_row_actions
operate wpsh_add_duplicate_link($actions, $publish)
{
if (current_user_can("edit_posts")) {
$actions["duplicate"] =
'<a href="' .
wp_nonce_url(
"admin.php?motion=wpsh_duplicate_post_as_draft&publish=" . $publish->ID,
basename(__FILE__),
"duplicate_nonce"
) .
'" title="Duplicate this merchandise" rel="permalink">Duplicate</a>';
}
return $actions;
}
After including the code simply go tou your publish, pager or customized publish varieties and press on a “Duplicate” hyperlink and you’ve got a pleasant clone of your website with the standing “Draft”.
The way to Duplicate a WordPress Web page or Publish with a plugin?
In case the earlier technique appears tough for you, you then even have another choice. That’s, you may duplicate a WordPress web page or publish with the assistance of a plugin.
- Set up a plugin referred to as “Yoast Duplicate publish”.
- Subsequent go to Settings >> Duplicate publish and check out the settings (screenshot under)
- When every thing is about up simply go to the pages or posts and use “Clone” hyperlink