Last active
December 24, 2024 06:53
-
-
Save marklchaves/75780b2d7ba9c9508e6da586147a859f to your computer and use it in GitHub Desktop.
1) Turn on the ACF shortcode if you have a new install of ACF and bypass the non-public post check. 2) If the shortcode doesn't work, use a Popup Maker filter to tack on the popup's ACF if it has one. I.e., use 1 or the other not both.
This file contains hidden or 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 // Ignore this first line when copying to your child theme's functions.php file. | |
/** Try using the ACF shortcode first. */ | |
/** Turn on the ACF shortcode as of ACF 6.3.0. | |
* https://www.advancedcustomfields.com/resources/shortcode/#enabling */ | |
add_action( 'acf/init', 'set_acf_settings' ); | |
function set_acf_settings() { | |
acf_update_setting( 'enable_shortcode', true ); | |
} | |
/** Make the ACF fields available to non-public posts like popups as of ACF 6.3.4. | |
* https://github.com/AdvancedCustomFields/acf/blob/693e8defb70e35270a4636579ad3c5dc64521bc3/includes/api/api-template.php#L1051*/ | |
add_filter( 'acf/shortcode/prevent_access_to_fields_on_non_public_posts', '__return_false' ); |
This file contains hidden or 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 // Ignore this first line when copying to your child theme's functions.php file. | |
/** If the ACF shortcode doesn't work, use this filter as a last resort. */ | |
/** Use a Popup Maker filter to tack on the popup's ACF if it has one. */ | |
add_filter( 'pum_popup_content', function( $id ) { | |
$id = pum_get_popup_id(); // Get the popup ID. | |
$post = get_post( $id ); // Get the popup post type. | |
// Extract the original popup content. | |
$the_content = apply_filters( 'the_content', $post->post_content ); | |
// Get the ACF value in the popup and sanitize it for XSS. | |
// - Change "test" to the name of your ACF. | |
// - Add security and more checks for malicious hacks as you need. | |
$acf_value = esc_html( get_post_meta( $id, 'test', true ) ); | |
// Format the presentation of the ACF value. If there isn't a value, show a message or alternate text. | |
$new_content = '<strong>Popup ID</strong>: ' . | |
$id . | |
'. <strong>ACF</strong>: ' . | |
( $acf_value ? $acf_value : 'Whoops, no ACF value!' ); | |
// Return the original content plus the ACF content. | |
return $the_content . $new_content; | |
} ); | |
// You can add the PHP code snippet to your child theme's <code>functions.php</code> file or | |
// with a third-party plugin such as <a href='https://wordpress.org/plugins/my-custom-functions/'>My Custom Functions</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Allow Access to Value in Editor UI Setting
Must turn this on for the ACF shortcode.