Created
May 24, 2023 21:48
-
-
Save mcaskill/3812b96b7795c3d31c36ad542f454ce9 to your computer and use it in GitHub Desktop.
WP / ACF: Ensure ACF fields can be previewed when post revisions are disabled.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Ensure ACF fields can be previewed when post revisions are disabled | |
* (`WP_POST_REVISIONS`). | |
* | |
* @listens action:wp_revisions_to_keep | |
* Filters the number of revisions to save for the given post. | |
*/ | |
add_filter( 'wp_revisions_to_keep', function ( int $num, WP_Post $post ) : int { | |
// Bail early if post revisions enabled. | |
if ( WP_POST_REVISIONS ) { | |
return $num; | |
} | |
// Bail early if the post type is not supported. | |
if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { | |
return $num; | |
} | |
// Bail early if not previewing post. | |
if ( | |
! is_preview() || | |
empty( $_GET['preview_id'] ) | |
) { | |
return $num; | |
} | |
$preview_id = (int) $_GET['preview_id']; | |
// Bail early if not previewing this post. | |
if ( $preview_id !== $post->ID ) { | |
return $num; | |
} | |
// Bail early if not currently filtering the post ID | |
// for ACF to fetch from its latest revision. | |
if ( | |
! doing_filter( 'wp_revisions_to_keep' ) || | |
! doing_filter( 'acf/validate_post_id' ) | |
) { | |
return $num; | |
} | |
return 1; | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment