Advertisement
colin123

WordPress Duplicate Page Code

Jan 15th, 2019
1,411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.05 KB | None | 0 0
  1. /*
  2.  * This code duplicates a WordPress page. The duplicate page will appear as a draft and the user will be redirected to the edit 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.     if ( !isset( $_GET['duplicate_nonce'] ) || !wp_verify_nonce( $_GET['duplicate_nonce'], basename( __FILE__ ) ) )
  11.         return;
  12.  
  13.     $post_id = (isset($_GET['post']) ? absint( $_GET['post'] ) : absint( $_POST['post'] ) );
  14.     $post = get_post( $post_id );
  15.  
  16.     $current_user = wp_get_current_user();
  17.     $new_post_author = $current_user->ID;
  18.  
  19.     if (isset( $post ) && $post != null) {
  20.  
  21.         $args = array(
  22.             'comment_status' => $post->comment_status,
  23.             'ping_status'    => $post->ping_status,
  24.             'post_author'    => $new_post_author,
  25.             'post_content'   => $post->post_content,
  26.             'post_excerpt'   => $post->post_excerpt,
  27.             'post_name'      => $post->post_name,
  28.             'post_parent'    => $post->post_parent,
  29.             'post_password'  => $post->post_password,
  30.             'post_status'    => 'draft',
  31.             'post_title'     => $post->post_title,
  32.             'post_type'      => $post->post_type,
  33.             'to_ping'        => $post->to_ping,
  34.             'menu_order'     => $post->menu_order
  35.         );
  36.  
  37.         $new_post_id = wp_insert_post( $args );
  38.  
  39.         $taxonomies = get_object_taxonomies($post->post_type); // returns array of taxonomy names for post type, ex array("category", "post_tag");
  40.         foreach ($taxonomies as $taxonomy) {
  41.             $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'slugs'));
  42.             wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);
  43.         }
  44.  
  45.         $post_meta_infos = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id");
  46.         if (count($post_meta_infos)!=0) {
  47.             $sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) ";
  48.             foreach ($post_meta_infos as $meta_info) {
  49.                 $meta_key = $meta_info->meta_key;
  50.                 if( $meta_key == '_wp_old_slug' ) continue;
  51.                 $meta_value = addslashes($meta_info->meta_value);
  52.                 $sql_query_sel[]= "SELECT $new_post_id, '$meta_key', '$meta_value'";
  53.             }
  54.             $sql_query.= implode(" UNION ALL ", $sql_query_sel);
  55.             $wpdb->query($sql_query);
  56.         }
  57.  
  58.  
  59.         wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
  60.         exit;
  61.     } else {
  62.         wp_die('Post creation failed, could not find original post: ' . $post_id);
  63.     }
  64. }
  65. add_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );
  66.  
  67. function rd_duplicate_post_link( $actions, $post ) {
  68.     if (current_user_can('edit_posts')) {
  69.         $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&post=' . $post->ID, basename(__FILE__), 'duplicate_nonce' ) . '" title="Duplicate this item" rel="permalink">Duplicate</a>';
  70.     }
  71.     return $actions;
  72. }
  73.  
  74. add_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );
  75. add_filter( 'page_row_actions', 'rd_duplicate_post_link', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement