Admin & Dashboard PHP Intermediate

Add a “Duplicate” Link to Posts and Pages

Copy any post or page, with its content, categories, tags and custom fields, into a new draft with one click.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 52 lines
add_filter( 'post_row_actions', 'nahnu_duplicate_link', 10, 2 );
add_filter( 'page_row_actions', 'nahnu_duplicate_link', 10, 2 );

function nahnu_duplicate_link( array $actions, WP_Post $post ): array {
	// WooCommerce has its own Duplicate link for products.
	if ( 'product' !== $post->post_type && current_user_can( 'edit_post', $post->ID ) ) {
		$url = wp_nonce_url( admin_url( 'admin.php?action=nahnu_duplicate&post=' . $post->ID ), 'nahnu_duplicate_' . $post->ID );
		$actions['nahnu_duplicate'] = '<a href="' . esc_url( $url ) . '">Duplicate</a>';
	}
	return $actions;
}

add_action( 'admin_action_nahnu_duplicate', function (): void {
	$id = absint( $_GET['post'] ?? 0 );
	check_admin_referer( 'nahnu_duplicate_' . $id );

	$post = get_post( $id );
	if ( ! $post || ! current_user_can( 'edit_post', $id ) ) {
		wp_die( 'You can\'t duplicate this item.' );
	}

	$new_id = wp_insert_post( wp_slash( [
		'post_type'      => $post->post_type,
		'post_title'     => $post->post_title . ' (copy)',
		'post_content'   => $post->post_content,
		'post_excerpt'   => $post->post_excerpt,
		'post_parent'    => $post->post_parent,
		'menu_order'     => $post->menu_order,
		'comment_status' => $post->comment_status,
		'ping_status'    => $post->ping_status,
		'post_status'    => 'draft',
		'post_author'    => get_current_user_id(),
	] ), true );
	if ( is_wp_error( $new_id ) ) {
		wp_die( esc_html( $new_id->get_error_message() ) );
	}

	foreach ( get_object_taxonomies( $post->post_type ) as $taxonomy ) {
		wp_set_object_terms( $new_id, wp_get_object_terms( $id, $taxonomy, [ 'fields' => 'ids' ] ), $taxonomy );
	}
	foreach ( get_post_meta( $id ) as $key => $values ) {
		if ( in_array( $key, [ '_edit_lock', '_edit_last', '_wp_old_slug', '_wp_old_date' ], true ) ) {
			continue;
		}
		foreach ( $values as $value ) {
			add_post_meta( $new_id, $key, wp_slash( maybe_unserialize( $value ) ) );
		}
	}

	wp_safe_redirect( admin_url( 'post.php?action=edit&post=' . $new_id ) );
	exit;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

Rebuilding a page layout from scratch, or copying a post to use as a template, takes time. This snippet adds a Duplicate link to every post and page in wp-admin.

How it works

The post_row_actions and page_row_actions filters add the link to the hover actions in the list. It points to admin.php?action=nahnu_duplicate with a nonce, so the copy can only be triggered on purpose by someone who can edit the original.

The handler copies the title (with “(copy)” added), content, excerpt, parent, menu order and discussion settings into a new draft owned by you, then copies every taxonomy term and custom field. It skips internal fields like edit locks and old slugs, then opens the copy in the editor.

Custom fields include page builder data, so layouts built with Bricks, Elementor and similar builders are copied too.

Things to watch

  • WooCommerce products are skipped because WooCommerce’s own Duplicate link also copies stock, variations and lookup tables.
  • Featured images are copied, since they are stored as a custom field. The media files themselves aren’t duplicated.

How to test it

Hover over a post in Posts → All Posts and click Duplicate. You should land in the editor with a draft called “… (copy)” with the same content and categories.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only