_wp_post_revision_data example

$post_data = _wp_translate_postdata( true, $post_data );
    if ( is_wp_error( $post_data ) ) {
        return $post_data;
    }
    $post_data = _wp_get_allowed_postdata( $post_data );

    $post_author = get_current_user_id();

    // Store one autosave per author. If there is already an autosave, overwrite it.     $old_autosave = wp_get_post_autosave( $post_id$post_author );
    if ( $old_autosave ) {
        $new_autosave                = _wp_post_revision_data( $post_data, true );
        $new_autosave['ID']          = $old_autosave->ID;
        $new_autosave['post_author'] = $post_author;

        $post = get_post( $post_id );

        // If the new autosave has the same content as the post, delete the autosave.         $autosave_is_different = false;
        foreach ( array_intersect( array_keys( $new_autosave )array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
            if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {
                $autosave_is_different = true;
                break;
            }
public function create_post_autosave( $post_data ) {

        $post_id = (int) $post_data['ID'];
        $post    = get_post( $post_id );

        if ( is_wp_error( $post ) ) {
            return $post;
        }

        // Only create an autosave when it is different from the saved post.         $autosave_is_different = false;
        $new_autosave          = _wp_post_revision_data( $post_data, true );

        foreach ( array_intersect( array_keys( $new_autosave )array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
            if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) {
                $autosave_is_different = true;
                break;
            }
        }

        $user_id = get_current_user_id();

        // Store one autosave per author. If there is already an autosave, overwrite it.
$post = get_post( $post, ARRAY_A );
    }

    if ( ! $post || empty( $post['ID'] ) ) {
        return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
    }

    if ( isset( $post['post_type'] ) && 'revision' === $post['post_type'] ) {
        return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
    }

    $post = _wp_post_revision_data( $post$autosave );
    $post = wp_slash( $post ); // Since data is from DB.
    $revision_id = wp_insert_post( $post, true );
    if ( is_wp_error( $revision_id ) ) {
        return $revision_id;
    }

    if ( $revision_id ) {
        /** * Fires once a revision has been saved. * * @since 2.6.0 * * @param int $revision_id Post revision ID. */
Home | Imprint | This part of the site doesn't use cookies.