Created
July 9, 2025 11:54
-
-
Save marklchaves/82f983a42432fbbf609b4f4bfbfc0b68 to your computer and use it in GitHub Desktop.
Grab a comma separated list of popup IDs from a specific page's or post's ACF field.
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 | |
| /** | |
| * PHP to get popup IDs from ACF and render them in a data attribute. | |
| * | |
| * This code should be placed in your theme's header.php, footer.php, | |
| * or a specific template file, usually within the <body> but **before** | |
| * the JS code that uses the popup IDs. | |
| * | |
| * This code example creates a <div> to store the popup IDs just before | |
| * the popup's HTML. It does that because the hook's priority is set to 10. | |
| * Where Popup Maker's hook priorities are set to 500. | |
| */ | |
| add_action( 'wp_footer', function () { | |
| // Define the specific Post ID you want to target. You can change | |
| // this value to the ID of the WordPress Post or Page where your | |
| // ACF 'popup_ids' field is saved. | |
| $my_post_id = 2; // CHANGE THIS TO YOUR POST/PAGE ID | |
| // Check if ACF's get_field function exists (ACF is active). | |
| if (function_exists('get_field')) { | |
| // Get the value of the 'popup_ids' field (1st arg) for the specified post/page (2nd arg). | |
| // The third argument 'false' means it won't format the value, just return raw. | |
| $popup_ids_string = get_field('popup_ids', $my_post_id, false); | |
| // Sanitize the string to ensure it's safe for HTML attribute. | |
| // We expect comma-separated IDs, so we'll sanitize each segment. | |
| $clean_popup_ids = ''; | |
| if (! empty($popup_ids_string)) { | |
| $ids_array = explode(',', $popup_ids_string); | |
| $sanitized_ids = array_map('sanitize_text_field', $ids_array); | |
| $clean_popup_ids = implode(',', $sanitized_ids); | |
| } | |
| // Output a hidden div with the data attribute. You could place this | |
| // just inside the <body> tag or anywhere **before** your JS. | |
| echo '<div id="popup-config-data" data-popup-ids="' . esc_attr($clean_popup_ids) . '" style="display:none;"></div>'; | |
| } | |
| }, 10 ); | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example JS for extracting the popup IDs from the and saving them in a JS array
Example input
Example output