get_delete_post_link( int|WP_Post $post, string $deprecated = , bool $force_delete = false ): string|void

Retrieves the delete posts link for post.

Description

Can be used within the WordPress loop or outside of it, with any post type.

Parameters

$postint|WP_Postoptional
Post ID or post object. Default is the global $post.
$deprecatedstringoptional
Not used.

Default:''

$force_deletebooloptional
Whether to bypass Trash and force deletion.

Default:false

Return

string|void The delete post link URL for the given post.

Source

function get_delete_post_link( $post = 0, $deprecated = '', $force_delete = false ) {
	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '3.0.0' );
	}

	$post = get_post( $post );

	if ( ! $post ) {
		return;
	}

	$post_type_object = get_post_type_object( $post->post_type );

	if ( ! $post_type_object ) {
		return;
	}

	if ( ! current_user_can( 'delete_post', $post->ID ) ) {
		return;
	}

	$action = ( $force_delete || ! EMPTY_TRASH_DAYS ) ? 'delete' : 'trash';

	$delete_link = add_query_arg( 'action', $action, admin_url( sprintf( $post_type_object->_edit_link, $post->ID ) ) );

	/**
	 * Filters the post delete link.
	 *
	 * @since 2.9.0
	 *
	 * @param string $link         The delete link.
	 * @param int    $post_id      Post ID.
	 * @param bool   $force_delete Whether to bypass the Trash and force deletion. Default false.
	 */
	return apply_filters( 'get_delete_post_link', wp_nonce_url( $delete_link, "$action-post_{$post->ID}" ), $post->ID, $force_delete );
}

Hooks

apply_filters( ‘get_delete_post_link’, string $link, int $post_id, bool $force_delete )

Filters the post delete link.

Changelog

VersionDescription
2.9.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Be careful, if you use this function with a custom post type, you must define the “show_ui” parameter of the register_post_type() function to true.

    With “show_ui” parameter to false, link doesn’t work
    https://example.com/wp-admin/?action=delete&_wpnonce=bb70aa97e2

    With “show_ui” parameter to true, link works
    https://example.com/wp-admin/post.php?post=1234&action=delete&_wpnonce=87d5108013

You must log in before being able to contribute a note or feedback.