wp_create_post_autosave

函数
wp_create_post_autosave ( $post_data )
参数
  • (array|int) $post_data Associative array containing the post data, or integer post ID. If a numeric post ID is provided, will use the `$_POST` superglobal.
    Required:
返回值
  • (int|WP_Error) The autosave revision ID. WP_Error or 0 on error.
定义位置
相关方法
wp_get_post_autosavewp_is_post_autosavewp_set_post_catswp_get_post_catswp_reset_postdata
引入
2.6.0
弃用
-

wp_create_post_autosave: 这是一个为一个文章创建自动保存的函数。它可以用来在用户编辑时自动保存对文章的修改。

从`$_POST`数据中为指定的文章创建自动保存数据。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_create_post_autosave( $post_data ) {
if ( is_numeric( $post_data ) ) {
$post_id = $post_data;
$post_data = $_POST;
} else {
$post_id = (int) $post_data['post_ID'];
}
$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;
}
}
if ( ! $autosave_is_different ) {
wp_delete_post_revision( $old_autosave->ID );
return 0;
}
/**
* Fires before an autosave is stored.
*
* @since 4.1.0
*
* @param array $new_autosave Post array - the autosave that is about to be saved.
*/
do_action( 'wp_creating_autosave', $new_autosave );
return wp_update_post( $new_autosave );
}
// _wp_put_post_revision() expects unescaped.
$post_data = wp_unslash( $post_data );
// Otherwise create the new autosave as a special post revision.
return _wp_put_post_revision( $post_data, true );
}
function wp_create_post_autosave( $post_data ) { if ( is_numeric( $post_data ) ) { $post_id = $post_data; $post_data = $_POST; } else { $post_id = (int) $post_data['post_ID']; } $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; } } if ( ! $autosave_is_different ) { wp_delete_post_revision( $old_autosave->ID ); return 0; } /** * Fires before an autosave is stored. * * @since 4.1.0 * * @param array $new_autosave Post array - the autosave that is about to be saved. */ do_action( 'wp_creating_autosave', $new_autosave ); return wp_update_post( $new_autosave ); } // _wp_put_post_revision() expects unescaped. $post_data = wp_unslash( $post_data ); // Otherwise create the new autosave as a special post revision. return _wp_put_post_revision( $post_data, true ); }
function wp_create_post_autosave( $post_data ) {
	if ( is_numeric( $post_data ) ) {
		$post_id   = $post_data;
		$post_data = $_POST;
	} else {
		$post_id = (int) $post_data['post_ID'];
	}

	$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;
			}
		}

		if ( ! $autosave_is_different ) {
			wp_delete_post_revision( $old_autosave->ID );
			return 0;
		}

		/**
		 * Fires before an autosave is stored.
		 *
		 * @since 4.1.0
		 *
		 * @param array $new_autosave Post array - the autosave that is about to be saved.
		 */
		do_action( 'wp_creating_autosave', $new_autosave );

		return wp_update_post( $new_autosave );
	}

	// _wp_put_post_revision() expects unescaped.
	$post_data = wp_unslash( $post_data );

	// Otherwise create the new autosave as a special post revision.
	return _wp_put_post_revision( $post_data, true );
}

常见问题

FAQs
查看更多 >