2
votes

I'm trying to add the "duplicate" post option to my custom post type admin menu called events. I have searched online, but there is very little documentation on how to add this function to the custom post types. Perhaps I am using the incorrect terminology when searching.

The code below adds the "duplicate" function, but when clicked it doesn't actually duplicate the post but returns a white screen instead. Are there any pointers or tips that you can give me?

function rd_duplicate_post_link( $actions, $post ) {
    if (current_user_can('edit_posts') || $post->post_type=='events') {
        $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>';
    }
    return $actions;
}

add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2)
1

1 Answers

3
votes

You need to call admin_action_rd_duplicate_post_as_draft hook

function rd_duplicate_post_link( $actions, $post ) {

    //print_r($actions);
    //if (current_user_can('edit_posts') || $post->post_type=='movies') {
        $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>';
   // }
    return $actions;
}

add_filter('page_row_actions', 'rd_duplicate_post_link', 10, 2);



add_action( 'admin_action_rd_duplicate_post_as_draft', 'dt_dpp_post_as_draft' ); 

  function dt_dpp_post_as_draft()
    {
          global $wpdb;

          /*sanitize_GET POST REQUEST*/
          $post_copy = sanitize_text_field( $_POST["post"] );
          $get_copy = sanitize_text_field( $_GET['post'] );
          $request_copy = sanitize_text_field( $_REQUEST['action'] );

          $opt = get_option('dpp_wpp_page_options');
          $suffix = !empty($opt['dpp_post_suffix']) ? ' -- '.$opt['dpp_post_suffix'] : '';
          $post_status = !empty($opt['dpp_post_status']) ? $opt['dpp_post_status'] : 'draft';
          $redirectit = !empty($opt['dpp_post_redirect']) ? $opt['dpp_post_redirect'] : 'to_list';

            if (! ( isset( $get_copy ) || isset( $post_copy ) || ( isset($request_copy) && 'dt_dpp_post_as_draft' == $request_copy ) ) ) {
            wp_die('No post!');
            }
            $returnpage = '';

            /* Get post id */
            $post_id = (isset($get_copy) ? $get_copy : $post_copy );

            $post = get_post( $post_id );

            $current_user = wp_get_current_user();
            $new_post_author = $current_user->ID;

            /*Create the post Copy */
            if (isset( $post ) && $post != null) {
                /* Post data array */
                $args = array('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' => $post_status,
                'post_title' => $post->post_title.$suffix,
                'post_type' => $post->post_type,
                'to_ping' => $post->to_ping,
                'menu_order' => $post->menu_order

               );
               $new_post_id = wp_insert_post( $args );

               $taxonomies = get_object_taxonomies($post->post_type);
               if(!empty($taxonomies) && is_array($taxonomies)):
               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);}
               endif;

               $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);
                  }

                 /*choice redirect */
                 if($post->post_type != 'post'):$returnpage = '?post_type='.$post->post_type;  endif;
                 if(!empty($redirectit) && $redirectit == 'to_list'):wp_redirect( admin_url( 'edit.php'.$returnpage ) );
                 elseif(!empty($redirectit) && $redirectit == 'to_page'):wp_redirect( admin_url( 'post.php?action=edit&post=' . $new_post_id ) );
                 else:
                 wp_redirect( admin_url( 'edit.php'.$returnpage ) );
                 endif;
                 exit;
                 } else {
                 wp_die('Error! Post creation failed: ' . $post_id);
                 }
   }