Skip to content

Instantly share code, notes, and snippets.

@smchenrybc
Last active January 17, 2024 18:28
Show Gist options
  • Save smchenrybc/307d63adbcdc4fdef4484e1b80278810 to your computer and use it in GitHub Desktop.
Save smchenrybc/307d63adbcdc4fdef4484e1b80278810 to your computer and use it in GitHub Desktop.
Bug fixes in functions.php
<?php
/**
* functions.php
*/
/**
* Fix a long-standing issue with ACF, where fields sometimes aren't shown
* in previews (ie. from Preview > Open in new tab).
*
* @link https://support.advancedcustomfields.com/forums/topic/custom-fields-on-post-preview/
*/
if ( class_exists( 'acf_revisions' ) ) {
$acf_revs_cls = acf()->revisions;
remove_filter( 'acf/validate_post_id', array( $acf_revs_cls, 'acf_validate_post_id', 10 ) );
}
/**
* Ensure ACF fields can be previewed when post revisions are disabled
*
* @link https://gist.github.com/mcaskill/3812b96b7795c3d31c36ad542f454ce9
*/
function bc_fix_acf_post_revisions( int $num, WP_Post $post ) {
if ( WP_POST_REVISIONS ) {
return $num;
}
if ( !post_type_supports( $post->post_type, 'revisions' ) ) {
return $num;
}
if ( !is_preview() || empty( $_GET['preview_id'] ) ) {
return $num;
}
$preview_id = (int) $_GET['preview_id'];
if ( $preview_id !== $post->ID ) {
return $num;
}
if ( !doing_filter( 'wp_revisions_to_keep' ) || !doing_filter( 'acf/validate_post_id' ) ) {
return $num;
}
return 1;
}
add_filter( 'wp_revisions_to_keep', 'bc_fix_acf_post_revisions', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment