Advertisement
Guest User

Untitled

a guest
Aug 17th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.41 KB | None | 0 0
  1. /*
  2.  * Function creates post duplicate as a draft and redirects then to the edit post screen
  3.  */
  4. function rd_duplicate_post_as_draft(){
  5.     global $wpdb;
  6.     if (! ( isset( $_GET['post']) || isset( $_POST['post'])  || ( isset($_REQUEST['action']) && 'rd_duplicate_post_as_draft' == $_REQUEST['action'] ) ) ) {
  7.         wp_die('No post to duplicate has been supplied!');
  8.     }
  9.  
  10.     /*
  11.      * get the original post id
  12.      */
  13.     $post_id = (isset($_GET['post']) ? $_GET['post'] : $_POST['post']);
  14.     /*
  15.      * and all the original post data then
  16.      */
  17.     $post = get_post( $post_id );
  18.  
  19.     /*
  20.      * if you don't want current user to be the new post author,
  21.      * then change next couple of lines to this: $new_post_author = $post->post_author;
  22.      */
  23.     $current_user = wp_get_current_user();
  24.     $new_post_author = $current_user->ID;
  25.  
  26.     /*
  27.      * if post data exists, create the post duplicate
  28.      */
  29.     if (isset( $post ) && $post != null && ! isset( $verify )) {
  30.  
  31.         /*
  32.          * new post data array
  33.          */
  34.         $args = array(
  35.             'comment_status' => $post->comment_status,
  36.             'ping_status'    => $post->ping_status,
  37.             'post_author'    => $new_post_author,
  38.             'post_content'   => $post->post_content,
  39.             'post_excerpt'   => $post->post_excerpt,
  40.             'post_name'      => $post->post_name,
  41.             'post_parent'    => $post->post_parent,
  42.             'post_password'  => $post->post_password,
  43.             'post_status'    => 'draft',
  44.             'post_title'     => $post->post_title,
  45.             'post_type'      => $post->post_type,
  46.             'to_ping'        => $post->to_ping,
  47.             'menu_order'     => $post->menu_order
  48.         );
  49.  
  50.         /*
  51.          * insert the post by wp_insert_post() function
  52.          */
  53.         $new_post_id = wp_insert_post( $args );
  54.  
  55.         /*
  56.          * get all current post terms ad set them to the new post draft
  57.          */
  58.         $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
  59.         foreach ($taxonomies as $taxonomy) {
  60.             $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
  61.             wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
  62.         }
  63.  
  64.         /*
  65.          * duplicate all post meta
  66.          */
  67.         $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
  68.         if (count($post_meta_infos)!=0) {
  69.             $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
  70.             foreach ($post_meta_infos as $meta_info) {
  71.                 $meta_key = $meta_info->meta_key;
  72.                 $meta_value = addslashes($meta_info->meta_value);
  73.                 $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
  74.             }
  75.             $sql_query.= implode(" UNION ALL ", $sql_query_sel);
  76.             $wpdb->query($sql_query);
  77.         }
  78.  
  79.         $verify = "";
  80.  
  81.         /*
  82.          * finally, redirect to the edit post screen for the new draft
  83.          */
  84.         wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
  85.         exit;
  86.     } else {
  87.         wp_die('Post creation failed, could not find original post: ' . $post_id);
  88.     }
  89. }
  90. add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );
  91.  
  92. /*
  93.  * Add the duplicate link to action list for post_row_actions
  94.  */
  95. function rd_duplicate_post_link( $actions, $post ) {
  96.     if (current_user_can('edit_posts')) {
  97.         $actions['duplicate'] = '<a href="admin.php?action=rd_duplicate_post_as_draft&amp;post=' . $post->ID . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
  98.     }
  99.     return $actions;
  100. }
  101.  
  102. add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement