|  |  | 
|  | <?php
 | 
|  | 
 | 
|  | add_filter( 'post_row_actions', 'wpcode_snippet_duplicate_post_link', 10, 2 );
 | 
|  | add_filter( 'page_row_actions', 'wpcode_snippet_duplicate_post_link', 10, 2 );
 | 
|  | 
 | 
|  | 
 | 
|  | if ( ! function_exists( 'wpcode_snippet_duplicate_post_link' ) ) {
 | 
|  | 	
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 	function wpcode_snippet_duplicate_post_link( $actions, $post ) {
 | 
|  | 
 | 
|  | 		
 | 
|  | 		$post_type_object = get_post_type_object( $post->post_type );
 | 
|  | 
 | 
|  | 		if ( null === $post_type_object || ! current_user_can( $post_type_object->cap->create_posts ) ) {
 | 
|  | 			return $actions;
 | 
|  | 		}
 | 
|  | 
 | 
|  | 
 | 
|  | 		$url = wp_nonce_url(
 | 
|  | 			add_query_arg(
 | 
|  | 				array(
 | 
|  | 					'action'  => 'wpcode_snippet_duplicate_post',
 | 
|  | 					'post_id' => $post->ID,
 | 
|  | 				),
 | 
|  | 				'admin.php'
 | 
|  | 			),
 | 
|  | 			'wpcode_duplicate_post_' . $post->ID,
 | 
|  | 			'wpcode_duplicate_nonce'
 | 
|  | 		);
 | 
|  | 
 | 
|  | 		$actions['wpcode_duplicate'] = '<a href="' . $url . '" title="Duplicate item" rel="permalink">Duplicate</a>';
 | 
|  | 
 | 
|  | 		return $actions;
 | 
|  | 	}
 | 
|  | }
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | 
 | 
|  | add_action( 'admin_action_wpcode_snippet_duplicate_post', function () {
 | 
|  | 
 | 
|  | 	if ( empty( $_GET['post_id'] ) ) {
 | 
|  | 		wp_die( 'No post id set for the duplicate action.' );
 | 
|  | 	}
 | 
|  | 
 | 
|  | 	$post_id = absint( $_GET['post_id'] );
 | 
|  | 
 | 
|  | 	
 | 
|  | 	if ( ! isset( $_GET['wpcode_duplicate_nonce'] ) || ! wp_verify_nonce( $_GET['wpcode_duplicate_nonce'], 'wpcode_duplicate_post_' . $post_id ) ) {
 | 
|  | 		
 | 
|  | 		wp_die( 'The link you followed has expired, please try again.' );
 | 
|  | 	}
 | 
|  | 
 | 
|  | 	
 | 
|  | 	$post = get_post( $post_id );
 | 
|  | 
 | 
|  | 	
 | 
|  | 	if ( $post ) {
 | 
|  | 		$current_user = wp_get_current_user();
 | 
|  | 		$new_post     = array(
 | 
|  | 			'comment_status' => $post->comment_status,
 | 
|  | 			'menu_order'     => $post->menu_order,
 | 
|  | 			'ping_status'    => $post->ping_status,
 | 
|  | 			'post_author'    => $current_user->ID,
 | 
|  | 			'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 . ' (copy)',
 | 
|  | 			'post_type'      => $post->post_type,
 | 
|  | 			'to_ping'        => $post->to_ping,
 | 
|  | 		);
 | 
|  | 		
 | 
|  | 		$duplicate_id = wp_insert_post( $new_post );
 | 
|  | 		
 | 
|  | 		$taxonomies = get_object_taxonomies( get_post_type( $post ) );
 | 
|  | 		if ( $taxonomies ) {
 | 
|  | 			foreach ( $taxonomies as $taxonomy ) {
 | 
|  | 				$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
 | 
|  | 				wp_set_object_terms( $duplicate_id, $post_terms, $taxonomy );
 | 
|  | 			}
 | 
|  | 		}
 | 
|  | 		
 | 
|  | 		$post_meta = get_post_meta( $post_id );
 | 
|  | 		if ( $post_meta ) {
 | 
|  | 
 | 
|  | 			foreach ( $post_meta as $meta_key => $meta_values ) {
 | 
|  | 				if ( '_wp_old_slug' === $meta_key ) { 
 | 
|  | 					continue;
 | 
|  | 				}
 | 
|  | 				foreach ( $meta_values as $meta_value ) {
 | 
|  | 					add_post_meta( $duplicate_id, $meta_key, maybe_unserialize( $meta_value ) );
 | 
|  | 				}
 | 
|  | 			}
 | 
|  | 		}
 | 
|  | 
 | 
|  | 		
 | 
|  | 		wp_safe_redirect(
 | 
|  | 			add_query_arg(
 | 
|  | 				array(
 | 
|  | 					'action' => 'edit',
 | 
|  | 					'post'   => $duplicate_id
 | 
|  | 				),
 | 
|  | 				admin_url( 'post.php' )
 | 
|  | 			)
 | 
|  | 		);
 | 
|  | 		exit;
 | 
|  | 	} else {
 | 
|  | 		wp_die( 'Error loading post for duplication, please try again.' );
 | 
|  | 	}
 | 
|  | } );
 | 
|  | 
 | 
|  |  | 
|  |  | 
THANKS, as I was using a plugin for only its duplicate function, that came with a whole lot of other unused features and obvious overheads that this snippet now eliminated.