Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Meeting page
- Populate dropdown with list of committees
- Use AJAX to get location associated with committee and populate as default value (may be overwritten by user)
- Properly save settings
- */
- // 1. Populates committee choices
- function acf_load_committee_choices( $field ) {
- // reset choices
- $field['choices'] = array();
- // loop through committees and put titles in array, excluding inactive committees
- $committees = get_posts(array(
- 'numberposts' => -1,
- 'post_type' => 'committee',
- 'meta_key' => 'committee_status',
- 'meta_value' => 'active',
- 'orderby' => 'menu_order',
- 'order' => 'ASC'
- ));
- foreach ($committees as $committee):
- $committee_names[] = get_field('committee_name', $committee);
- endforeach;
- // loop through array and add to field 'choices'
- if( is_array($committee_names) ) {
- foreach( $committee_names as $choice ) {
- $field['choices'][ $choice ] = $choice;
- }
- }
- // return the field
- return $field;
- }
- // specify that
- add_filter('acf/load_field/name=meeting_committee', 'acf_load_committee_choices');
- // 2. Enqueues script that makes AJAX request which will return list of areas for selected country.
- function acf_admin_enqueue( $hook ) {
- $type = get_post_type(); // Check current post type
- $types = array( 'meeting' ); // Allowed post types
- if( !in_array( $type, $types ) )
- return; // Only applies to post types in array
- wp_enqueue_script( 'populate-location', get_stylesheet_directory_uri() . '/js/autopopulate.js' );
- wp_localize_script( 'populate-location', 'pl_vars', array(
- 'pl_nonce' => wp_create_nonce( 'pl_nonce' ), // Create nonce which we later will use to verify AJAX request
- // Used to save settings
- 'selected_committee' => get_field('selected_committee', $current_post->ID),
- 'selected_location' => get_field('selected_location', $current_post->ID)
- )
- );
- }
- add_action( 'admin_enqueue_scripts', 'acf_admin_enqueue' );
- // 3. Returns default location for selected committee
- function location_by_committee( $selected_committee ) {
- // Verify nonce
- if( !isset( $_POST['pl_nonce'] ) || !wp_verify_nonce( $_POST['pl_nonce'], 'pl_nonce' ) )
- die('Permission denied');
- // Get committee var
- $selected_committee = $_POST['meeting_committee'];
- // Get names and locations from Committee pages—no need to filter here beyond post type.
- $committees = get_posts(array(
- 'numberposts' => -1,
- 'post_type' => 'committee'
- ));
- // Loop and get values
- foreach ($committees as $committee):
- $committee_names[] = get_field('committee_name', $committee);
- $committee_locations[] = get_field('committee_location', $committee);
- endforeach;
- // Combine name and location arrays
- $locations = array();
- // $locations = array_combine( $committee_names, $committee_locations ); // PHP 5+ only...should work, though
- // Alt version for < PHP 5
- foreach($locations as $key => $value):
- $locations[$committee_names] = $committee_locations;
- endforeach;
- /* Manual associative array to test if constructing array is the problem...but this doesn't work, either
- $locations = array(
- 'General Board' => 'boardroom',
- 'Land Use and Economic Development' => 'Community Center',
- 'Outreach' => 'home'
- );
- */
- // Returns default location by committee selected if selected committee exists in array
- if (array_key_exists( $selected_committee, $locations)) {
- // Convert areas to array
- $arr_data = explode( ', ', $locations[ $selected_committee ] );
- return wp_send_json($arr_data);
- } else {
- $arr_data = array();
- return wp_send_json($arr_data);
- }
- die();
- }
- add_action('wp_ajax_pl_add_location', 'location_by_committee');
- add_action('wp_ajax_nopriv_pl_add_location', 'location_by_committee');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement