Skip to content

Instantly share code, notes, and snippets.

@Sean-Spallen
Last active February 27, 2017 12:10
Show Gist options
  • Save Sean-Spallen/d7f1af8f87653a8181aa7cee0a77c33d to your computer and use it in GitHub Desktop.
Save Sean-Spallen/d7f1af8f87653a8181aa7cee0a77c33d to your computer and use it in GitHub Desktop.
Example code for passing an ACF options page variable to JavaScript through the wp_admin_footer
// holder is the ID of the invisible Div that holds the option field.
// Function to find the option_test text field and populate it with data that is echoed in the option_holder div
jQuery(document).ready(function () {
var option_populate_FIELD_NAME = document.getElementById('acf-field_58b2d845dda65');
var option_holder_FIELD_NAME = jQuery('#option_holder_FIELD_NAME');
var option_test_FIELD_NAME = option_holder_FIELD_NAME["0"].innerText;
// Checks that the field is empty before populating
if (option_populate_FIELD_NAME.value == "") {
option_populate_FIELD_NAME.value = option_FIELD_NAME;
}
});
// Function to find the option_test_wysiwyg WYSIWYG editor field and populate it with data that is echoed in the option_holder div
jQuery(document).ready(function () {
// Iframe takes longer to load than the rest of the page - wait a second
setTimeout(function () {
//Searches the page for the relevant iFrame
var option_holder_FIELD_NAME_wysiwyg = jQuery('#option_holder_wysiwyg');
var option_test_FIELD_NAMEe_wysiwyg = option_holder_wysiwyg["0"].innerText;
var option_populate_FIELD_NAME_id = document.querySelector('#mceu_146');
var iframe = option_populate_FIELD_NAME_id.querySelector('iframe');
//console.log(iframe)
var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
//console.log(iframeDocument);
var option_populate_FIELD_NAME_wysiwyg = iframeDocument.querySelector('body');
//console.log(option_populate_wysiwyg);
// Append item
option_populate_FIELD_NAME.append(option_FIELD_NAME);
//console.log(option_populate_wysiwyg.firstChild);
}, 1000);
});
// Enable options page
if( function_exists('acf_add_options_page') ) {
acf_add_options_page();
}
// Custom WordPress Footer
function remove_footer_admin () {
// Each empty div holds a php varible
echo '<div id="option_holder_FIELD_NAME" style="display:none">'; the_field('field_name', 'option'); echo '</div>';
echo '<div id="option_holder_FIELD_NAME_wysiwyg" style="display:none">'; the_field('field_name_wysiwyg', 'option'); echo '</div>';
}
// Enqueues scripts in specified CPT admin area
function add_admin_scripts( $hook ) {
global $post;
if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
if ( 'city' === $post->post_type ) {
// Enqueues JavaScript
wp_enqueue_script( 'myscript', get_stylesheet_directory_uri().'/assets/js/dynamic-option-fields.js' );
// Enqueues Custom Footer
add_filter('admin_footer_text', 'remove_footer_admin');
}
}
}
// Calls admin scripts function
add_action( 'admin_enqueue_scripts', 'add_admin_scripts', 10, 1 );
@Sean-Spallen
Copy link
Author

Fields are created on the options page and the post page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment