Home / Admin / Add duplicate link to admin bar menu
Duplicate Snippet

Embed Snippet on Your Site

Add duplicate link to admin bar menu

Extend the WPCode duplicate snippet (https://library.wpcode.com/snippet/7oqnzxow/) with an admin bar menu item. The duplicate snippet needs to be active for the duplication to work.

<10
Code Preview
php
<?php
add_action( 'admin_bar_menu', function ( $admin_bar ) {
	// Only show this when editing a post.
	$screen = get_current_screen();
	if ( ! $screen || 'post' !== $screen->base ) {
		return;
	}
	$post             = get_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;
	}
	$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'
	);
	$admin_bar->add_menu(
		array(
			'id'    => 'wpcode_duplicate',
			'title' => 'Duplicate',
			'href'  => $url,
		)
	);
}, 1100 );

Comments

Add a Comment