Advertisement
dougfitz

ACF field population functions

Aug 27th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. /*
  2.  
  3. Meeting page
  4. Populate dropdown with list of committees
  5. Use AJAX to get location associated with committee and populate as default value (may be overwritten by user)
  6. Properly save settings
  7.  
  8. */
  9.  
  10. // 1. Populates committee choices
  11. function acf_load_committee_choices( $field ) {
  12.  
  13. // reset choices
  14. $field['choices'] = array();
  15.  
  16. // loop through committees and put titles in array, excluding inactive committees
  17. $committees = get_posts(array(
  18. 'numberposts' => -1,
  19. 'post_type' => 'committee',
  20. 'meta_key' => 'committee_status',
  21. 'meta_value' => 'active',
  22. 'orderby' => 'menu_order',
  23. 'order' => 'ASC'
  24. ));
  25.  
  26. foreach ($committees as $committee):
  27. $committee_names[] = get_field('committee_name', $committee);
  28. endforeach;
  29.  
  30. // loop through array and add to field 'choices'
  31. if( is_array($committee_names) ) {
  32. foreach( $committee_names as $choice ) {
  33. $field['choices'][ $choice ] = $choice;
  34. }
  35. }
  36.  
  37. // return the field
  38. return $field;
  39. }
  40. // specify that
  41. add_filter('acf/load_field/name=meeting_committee', 'acf_load_committee_choices');
  42.  
  43.  
  44.  
  45. // 2. Enqueues script that makes AJAX request which will return list of areas for selected country.
  46.  
  47. function acf_admin_enqueue( $hook ) {
  48.  
  49. $type = get_post_type(); // Check current post type
  50. $types = array( 'meeting' ); // Allowed post types
  51.  
  52. if( !in_array( $type, $types ) )
  53. return; // Only applies to post types in array
  54.  
  55. wp_enqueue_script( 'populate-location', get_stylesheet_directory_uri() . '/js/autopopulate.js' );
  56.  
  57. wp_localize_script( 'populate-location', 'pl_vars', array(
  58. 'pl_nonce' => wp_create_nonce( 'pl_nonce' ), // Create nonce which we later will use to verify AJAX request
  59. // Used to save settings
  60. 'selected_committee' => get_field('selected_committee', $current_post->ID),
  61. 'selected_location' => get_field('selected_location', $current_post->ID)
  62. )
  63. );
  64. }
  65. add_action( 'admin_enqueue_scripts', 'acf_admin_enqueue' );
  66.  
  67.  
  68.  
  69. // 3. Returns default location for selected committee
  70.  
  71. function location_by_committee( $selected_committee ) {
  72.  
  73. // Verify nonce
  74. if( !isset( $_POST['pl_nonce'] ) || !wp_verify_nonce( $_POST['pl_nonce'], 'pl_nonce' ) )
  75. die('Permission denied');
  76.  
  77. // Get committee var
  78. $selected_committee = $_POST['meeting_committee'];
  79.  
  80. // Get names and locations from Committee pages—no need to filter here beyond post type.
  81. $committees = get_posts(array(
  82. 'numberposts' => -1,
  83. 'post_type' => 'committee'
  84. ));
  85.  
  86. // Loop and get values
  87. foreach ($committees as $committee):
  88. $committee_names[] = get_field('committee_name', $committee);
  89. $committee_locations[] = get_field('committee_location', $committee);
  90. endforeach;
  91.  
  92. // Combine name and location arrays
  93. $locations = array();
  94.  
  95. // $locations = array_combine( $committee_names, $committee_locations ); // PHP 5+ only...should work, though
  96. // Alt version for < PHP 5
  97. foreach($locations as $key => $value):
  98. $locations[$committee_names] = $committee_locations;
  99. endforeach;
  100.  
  101.  
  102. /* Manual associative array to test if constructing array is the problem...but this doesn't work, either
  103. $locations = array(
  104. 'General Board' => 'boardroom',
  105. 'Land Use and Economic Development' => 'Community Center',
  106. 'Outreach' => 'home'
  107. );
  108. */
  109.  
  110.  
  111.  
  112. // Returns default location by committee selected if selected committee exists in array
  113. if (array_key_exists( $selected_committee, $locations)) {
  114.  
  115. // Convert areas to array
  116. $arr_data = explode( ', ', $locations[ $selected_committee ] );
  117. return wp_send_json($arr_data);
  118.  
  119. } else {
  120.  
  121. $arr_data = array();
  122. return wp_send_json($arr_data);
  123. }
  124.  
  125. die();
  126.  
  127. }
  128.  
  129. add_action('wp_ajax_pl_add_location', 'location_by_committee');
  130. add_action('wp_ajax_nopriv_pl_add_location', 'location_by_committee');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement