wp_ajax_trash_post()WP 3.1.0

Handles sending a post to the Trash via AJAX.

No Hooks.

Return

null. Nothing (null).

Usage

wp_ajax_trash_post( $action );
$action(string) (required)
Action to perform.

Changelog

Since 3.1.0 Introduced.

wp_ajax_trash_post() code WP 6.4.3

function wp_ajax_trash_post( $action ) {
	if ( empty( $action ) ) {
		$action = 'trash-post';
	}

	$id = isset( $_POST['id'] ) ? (int) $_POST['id'] : 0;
	check_ajax_referer( "{$action}_$id" );

	if ( ! current_user_can( 'delete_post', $id ) ) {
		wp_die( -1 );
	}

	if ( ! get_post( $id ) ) {
		wp_die( 1 );
	}

	if ( 'trash-post' === $action ) {
		$done = wp_trash_post( $id );
	} else {
		$done = wp_untrash_post( $id );
	}

	if ( $done ) {
		wp_die( 1 );
	}

	wp_die( 0 );
}