wp_autosave_post_revisioned_meta_fields

函式
wp_autosave_post_revisioned_meta_fields ( $new_autosave )
引數
  • (array) $new_autosave The new post data being autosaved.
    Required:
定義位置
相關方法
wp_save_revisioned_meta_fieldswp_post_revision_meta_keyswp_restore_post_revision_meta_wp_post_revision_fieldswp_save_post_revision_on_insert
引入
6.4.0
棄用
-

自動儲存已修訂的元欄位。

遍歷已修訂的元欄位,並檢查每個欄位是否已設定且值已更改。如果已設定,則儲存元值並將其附加到自動儲存中。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_autosave_post_revisioned_meta_fields( $new_autosave ) {
/*
* The post data arrives as either $_POST['data']['wp_autosave'] or the $_POST
* itself. This sets $posted_data to the correct variable.
*
* Ignoring sanitization to avoid altering meta. Ignoring the nonce check because
* this is hooked on inner core hooks where a valid nonce was already checked.
*/
$posted_data = isset( $_POST['data']['wp_autosave'] ) ? $_POST['data']['wp_autosave'] : $_POST;
$post_type = get_post_type( $new_autosave['post_parent'] );
/*
* Go thru the revisioned meta keys and save them as part of the autosave, if
* the meta key is part of the posted data, the meta value is not blank and
* the the meta value has changes from the last autosaved value.
*/
foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) {
if (
isset( $posted_data[ $meta_key ] ) &&
get_post_meta( $new_autosave['ID'], $meta_key, true ) !== wp_unslash( $posted_data[ $meta_key ] )
) {
/*
* Use the underlying delete_metadata() and add_metadata() functions
* vs delete_post_meta() and add_post_meta() to make sure we're working
* with the actual revision meta.
*/
delete_metadata( 'post', $new_autosave['ID'], $meta_key );
/*
* One last check to ensure meta value not empty().
*/
if ( ! empty( $posted_data[ $meta_key ] ) ) {
/*
* Add the revisions meta data to the autosave.
*/
add_metadata( 'post', $new_autosave['ID'], $meta_key, $posted_data[ $meta_key ] );
}
}
}
}
function wp_autosave_post_revisioned_meta_fields( $new_autosave ) { /* * The post data arrives as either $_POST['data']['wp_autosave'] or the $_POST * itself. This sets $posted_data to the correct variable. * * Ignoring sanitization to avoid altering meta. Ignoring the nonce check because * this is hooked on inner core hooks where a valid nonce was already checked. */ $posted_data = isset( $_POST['data']['wp_autosave'] ) ? $_POST['data']['wp_autosave'] : $_POST; $post_type = get_post_type( $new_autosave['post_parent'] ); /* * Go thru the revisioned meta keys and save them as part of the autosave, if * the meta key is part of the posted data, the meta value is not blank and * the the meta value has changes from the last autosaved value. */ foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) { if ( isset( $posted_data[ $meta_key ] ) && get_post_meta( $new_autosave['ID'], $meta_key, true ) !== wp_unslash( $posted_data[ $meta_key ] ) ) { /* * Use the underlying delete_metadata() and add_metadata() functions * vs delete_post_meta() and add_post_meta() to make sure we're working * with the actual revision meta. */ delete_metadata( 'post', $new_autosave['ID'], $meta_key ); /* * One last check to ensure meta value not empty(). */ if ( ! empty( $posted_data[ $meta_key ] ) ) { /* * Add the revisions meta data to the autosave. */ add_metadata( 'post', $new_autosave['ID'], $meta_key, $posted_data[ $meta_key ] ); } } } }
function wp_autosave_post_revisioned_meta_fields( $new_autosave ) {
	/*
	 * The post data arrives as either $_POST['data']['wp_autosave'] or the $_POST
	 * itself. This sets $posted_data to the correct variable.
	 *
	 * Ignoring sanitization to avoid altering meta. Ignoring the nonce check because
	 * this is hooked on inner core hooks where a valid nonce was already checked.
	 */
	$posted_data = isset( $_POST['data']['wp_autosave'] ) ? $_POST['data']['wp_autosave'] : $_POST;

	$post_type = get_post_type( $new_autosave['post_parent'] );

	/*
	 * Go thru the revisioned meta keys and save them as part of the autosave, if
	 * the meta key is part of the posted data, the meta value is not blank and
	 * the the meta value has changes from the last autosaved value.
	 */
	foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) {

		if (
		isset( $posted_data[ $meta_key ] ) &&
		get_post_meta( $new_autosave['ID'], $meta_key, true ) !== wp_unslash( $posted_data[ $meta_key ] )
		) {
			/*
			 * Use the underlying delete_metadata() and add_metadata() functions
			 * vs delete_post_meta() and add_post_meta() to make sure we're working
			 * with the actual revision meta.
			 */
			delete_metadata( 'post', $new_autosave['ID'], $meta_key );

			/*
			 * One last check to ensure meta value not empty().
			 */
			if ( ! empty( $posted_data[ $meta_key ] ) ) {
				/*
				 * Add the revisions meta data to the autosave.
				 */
				add_metadata( 'post', $new_autosave['ID'], $meta_key, $posted_data[ $meta_key ] );
			}
		}
	}
}

常見問題

FAQs
檢視更多 >