Advertisement
Guenni007

duplicate-posts-and-pages

Oct 26th, 2023
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.94 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @snippet  Duplicate posts and pages without plugins
  4.  * @author   Misha Rudrastyh
  5.  * @url      https://rudrastyh.com/wordpress/duplicate-post.html
  6.  */
  7.  
  8. // Add the duplicate link to action list for post_row_actions
  9. // for "post" and custom post types
  10. add_filter("post_row_actions", "rd_duplicate_post_link", 10, 2);
  11. // for "page" post type
  12. add_filter("page_row_actions", "rd_duplicate_post_link", 10, 2);
  13. // for "portfolio" post type ?
  14.  
  15.  
  16. function rd_duplicate_post_link($actions, $post)
  17. {
  18.     if (!current_user_can("edit_posts")) {
  19.         return $actions;
  20.     }
  21.  
  22.  
  23.  
  24.     $url = wp_nonce_url(
  25.         add_query_arg(
  26.             [
  27.                 "action" => "rd_duplicate_post_as_draft",
  28.                 "post" => $post->ID,
  29.             ],
  30.             "admin.php"
  31.         ),
  32.         basename(__FILE__),
  33.         "duplicate_nonce"
  34.     );
  35.     /****
  36.         Modification at https://oxygenados.com to avoid link creation in snippets Advanced Scripts, Scripts Organizer.....
  37.     **/
  38.     $myCat = get_the_category($post->ID);
  39.     $Posttype = get_post_type($post->ID);
  40.     if ($Posttype == "page" || $myCat[0]->name != "") {
  41.         /** Changes by https://oxygenados.com **/
  42.         $actions["duplicate"] =
  43.             /** Changes by Guenni007 - to be translatable **/
  44.             '<a href="' . $url . '" aria-label="' . \esc_attr( \sprintf( \__( 'Duplicate this entry or page', 'avia_framework' ) , $title )) . '">' . \esc_html__( 'Duplicate', 'avia_framework' ) . '</a>';
  45.     }
  46.     return $actions;
  47. }
  48.  
  49.  
  50. /*
  51.  * Function creates post duplicate as a draft and redirects then to the edit post screen
  52.  */
  53. add_action(
  54.     "admin_action_rd_duplicate_post_as_draft",
  55.     "rd_duplicate_post_as_draft"
  56. );
  57.  
  58. function rd_duplicate_post_as_draft()
  59. {
  60.     // check if post ID has been provided and action
  61.     if (empty($_GET["post"])) {
  62.         wp_die("No post to duplicate has been provided!");
  63.     }
  64.  
  65.     // Nonce verification
  66.     if (
  67.         !isset($_GET["duplicate_nonce"]) ||
  68.         !wp_verify_nonce($_GET["duplicate_nonce"], basename(__FILE__))
  69.     ) {
  70.         return;
  71.     }
  72.  
  73.     // Get the original post id
  74.     $post_id = absint($_GET["post"]);
  75.  
  76.     // And all the original post data then
  77.     $post = get_post($post_id);
  78.  
  79.     /*
  80.      * if you don't want current user to be the new post author,
  81.      * then change next couple of lines to this: $new_post_author = $post->post_author;
  82.      */
  83.     $current_user = wp_get_current_user();
  84.     $new_post_author = $current_user->ID;
  85.  
  86.     // if post data exists (I am sure it is, but just in a case), create the post duplicate
  87.     if ($post) {
  88.         // new post data array
  89.         $args = [
  90.             "comment_status" => $post->comment_status,
  91.             "ping_status" => $post->ping_status,
  92.             "post_author" => $new_post_author,
  93.             "post_content" => $post->post_content,
  94.             "post_excerpt" => $post->post_excerpt,
  95.             "post_name" => $post->post_name,
  96.             "post_parent" => $post->post_parent,
  97.             "post_password" => $post->post_password,
  98.             "post_status" => "draft",
  99.             "post_title" => $post->post_title,
  100.             "post_type" => $post->post_type,
  101.             "to_ping" => $post->to_ping,
  102.             "menu_order" => $post->menu_order,
  103.         ];
  104.  
  105.         // insert the post by wp_insert_post() function
  106.         $new_post_id = wp_insert_post($args);
  107.  
  108.         /*
  109.          * get all current post terms ad set them to the new post draft
  110.          */
  111.         $taxonomies = get_object_taxonomies(get_post_type($post)); // returns array of taxonomy names for post type, ex array("category", "post_tag");
  112.         if ($taxonomies) {
  113.             foreach ($taxonomies as $taxonomy) {
  114.                 $post_terms = wp_get_object_terms($post_id, $taxonomy, [
  115.                     "fields" => "slugs",
  116.                 ]);
  117.                 wp_set_object_terms(
  118.                     $new_post_id,
  119.                     $post_terms,
  120.                     $taxonomy,
  121.                     false
  122.                 );
  123.             }
  124.         }
  125.  
  126.         // duplicate all post meta
  127.         $post_meta = get_post_meta($post_id);
  128.         if ($post_meta) {
  129.             foreach ($post_meta as $meta_key => $meta_values) {
  130.                 if ("_wp_old_slug" == $meta_key) {
  131.                     // do nothing for this meta key
  132.                     continue;
  133.                 }
  134.  
  135.                 foreach ($meta_values as $meta_value) {
  136.                     add_post_meta($new_post_id, $meta_key, $meta_value);
  137.                 }
  138.             }
  139.         }
  140.  
  141.         // finally, redirect to the edit post screen for the new draft
  142.         // wp_safe_redirect(
  143.         //  add_query_arg(
  144.         //      array(
  145.         //          'action' => 'edit',
  146.         //          'post' => $new_post_id
  147.         //      ),
  148.         //      admin_url( 'post.php' )
  149.         //  )
  150.         // );
  151.         // exit;
  152.         // or we can redirect to all posts with a message
  153.         wp_safe_redirect(
  154.             add_query_arg(
  155.                 [
  156.                     "post_type" =>
  157.                         "post" !== get_post_type($post)
  158.                             ? get_post_type($post)
  159.                             : false,
  160.                     "saved" => "post_duplication_created", // just a custom slug here
  161.                 ],
  162.                 admin_url("edit.php")
  163.             )
  164.         );
  165.         exit();
  166.     } else {
  167.         wp_die("Copy creation has failed :(( ");
  168.     }
  169. }
  170.  
  171. /*
  172.  * In case we decided to add admin notices
  173.  */
  174. add_action("admin_notices", "rudr_duplication_admin_notice");
  175.  
  176. function rudr_duplication_admin_notice()
  177. {
  178.     // Get the current screen
  179.     $screen = get_current_screen();
  180.  
  181.     if ("edit" !== $screen->base) {
  182.         return;
  183.     }
  184.  
  185.     //Checks if settings updated
  186.     if (isset($_GET["saved"]) && "post_duplication_created" == $_GET["saved"]) {
  187.         echo '<div class="notice notice-success is-dismissible"><p>The copy has been created.</p></div>';
  188.     }
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement