wp_untrash_post example

$untrashed = 0;

            if ( isset( $_GET['doaction'] ) && ( 'undo' === $_GET['doaction'] ) ) {
                add_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10, 3 );
            }

            foreach ( (array) $post_ids as $post_id ) {
                if ( ! current_user_can( 'delete_post', $post_id ) ) {
                    wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
                }

                if ( ! wp_untrash_post( $post_id ) ) {
                    wp_die( __( 'Error in restoring the item from Trash.' ) );
                }

                $untrashed++;
            }
            $sendback = add_query_arg( 'untrashed', $untrashed$sendback );

            remove_filter( 'wp_untrash_post_status', 'wp_untrash_post_set_previous_status', 10 );

            break;
        case 'delete':
            
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 );
}

/** * Handles restoring a post from the Trash via AJAX. * * @since 3.1.0 * * @param string $action Action to perform. */

            break;
        case 'untrash':
            if ( empty( $post_ids ) ) {
                break;
            }
            foreach ( $post_ids as $post_id ) {
                if ( ! current_user_can( 'delete_post', $post_id ) ) {
                    wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
                }

                if ( ! wp_untrash_post( $post_id ) ) {
                    wp_die( __( 'Error in restoring the item from Trash.' ) );
                }
            }
            $location = add_query_arg( 'untrashed', count( $post_ids )$location );
            break;
        case 'delete':
            if ( empty( $post_ids ) ) {
                break;
            }
            foreach ( $post_ids as $post_id_del ) {
                if ( ! current_user_can( 'delete_post', $post_id_del ) ) {
                    
wp_die( __( 'The item you are trying to restore from the Trash no longer exists.' ) );
        }

        if ( ! $post_type_object ) {
            wp_die( __( 'Invalid post type.' ) );
        }

        if ( ! current_user_can( 'delete_post', $post_id ) ) {
            wp_die( __( 'Sorry, you are not allowed to restore this item from the Trash.' ) );
        }

        if ( ! wp_untrash_post( $post_id ) ) {
            wp_die( __( 'Error in restoring the item from Trash.' ) );
        }

        $sendback = add_query_arg(
            array(
                'untrashed' => 1,
                'ids'       => $post_id,
            ),
            $sendback
        );
        wp_redirect( $sendback );
        
Home | Imprint | This part of the site doesn't use cookies.