function acf_get_taxonomy_terms($taxonomies = array()) { // force array $taxonomies = acf_force_type_array($taxonomies); // get pretty taxonomy names $taxonomies = acf_get_pretty_taxonomies($taxonomies); // vars $r = array(); // populate $r foreach (array_keys($taxonomies) as $taxonomy) { // vars $label = $taxonomies[$taxonomy]; $terms = get_terms($taxonomy, array('hide_empty' => false)); if (!empty($terms)) { $r[$label] = array(); foreach ($terms as $term) { $k = "{$taxonomy}:{$term->slug}"; $r[$label][$k] = $term->name; } } } // return return $r; }
function render_field($field) { // echo "<pre>"; // print_r($field); // echo "</pre>"; $taxonomies = array(); $taxonomies = acf_get_array($taxonomies); $taxonomies = acf_get_pretty_taxonomies($taxonomies); $all_taxonomies = acf_get_taxonomy_terms(); $selected_taxonomies = array(); $terms = array(); $slug_name = !empty($field['choices']) ? $field['choices'] : array_keys(acf_get_pretty_taxonomies()); foreach ($slug_name as $k1 => $v1) { $terms = array_merge($terms, get_terms($v1, array('hide_empty' => false))); foreach ($taxonomies as $k2 => $v2) { if ($v1 == $k2) { $field['choices'][$k1] = $v2; } } } foreach ($field['choices'] as $k1 => $v1) { foreach ($all_taxonomies as $k2 => $v2) { if ($v1 == $k2) { $selected_taxonomies[$v1] = $all_taxonomies[$k2]; } } } $field['choices'] = $selected_taxonomies; // convert value to array // $field['value'] = acf_force_type_array($field['value']); // add empty value (allows '' to be selected) if (empty($field['value'])) { $field['value'][''] = ''; $field['value']['cat'] = ''; } // placeholder if (empty($field['placeholder'])) { $field['placeholder'] = __("Select", 'acf'); } // vars $atts = array('id' => $field['id'], 'class' => $field['class'] . ' js-multi-taxonomy-select2', 'name' => $field['name'], 'data-ui' => $field['ui'], 'data-ajax' => $field['ajax'], 'data-multiple' => $field['multiple'], 'data-placeholder' => $field['placeholder'], 'data-allow_null' => $field['allow_null']); // hidden input if ($field['ui']) { acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'], 'name' => $field['name'], 'value' => implode(',', $field['value']))); } elseif ($field['multiple']) { acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); } // ui if ($field['ui']) { $atts['disabled'] = 'disabled'; $atts['class'] .= ' acf-hidden'; } // multiple if ($field['multiple']) { $atts['multiple'] = 'multiple'; $atts['size'] = 5; $atts['name'] .= '[]'; } // special atts foreach (array('readonly', 'disabled') as $k) { if (!empty($field[$k])) { $atts[$k] = $k; } } // vars $els = array(); $choices = array(); if ($field['data_type']) { // loop through values and add them as options if (!empty($field['choices'])) { foreach ($field['choices'] as $k => $v) { if (is_array($v)) { // optgroup $els[] = array('type' => 'optgroup', 'label' => $k); if (!empty($v)) { foreach ($v as $k2 => $v2) { if ($field['type_value']) { foreach ($terms as $key => $val) { if ($val->name == $v2) { $els[] = array('type' => 'option', 'value' => $val->term_id, 'label' => $v2, 'selected' => $slct = $val->term_id == $field['value'] ? "selected" : ""); } } } else { $els[] = array('type' => 'option', 'value' => $k2, 'label' => $v2, 'selected' => $slct = $k2 == $field['value'] ? "selected" : ""); } $choices[] = $k2; } } $els[] = array('type' => '/optgroup'); } else { $els[] = array('type' => 'option', 'value' => $k, 'label' => $v, 'selected' => $slct = $k == $field['value'] ? "selected" : ""); $choices[] = $k; } } } // null if ($field['allow_null']) { array_unshift($els, array('type' => 'option', 'value' => '', 'label' => '- ' . $field['placeholder'] . ' -')); } // html echo '<select ' . acf_esc_attr($atts) . '>'; // construct html if (!empty($els)) { foreach ($els as $el) { // extract type $type = acf_extract_var($el, 'type'); if ($type == 'option') { // get label $label = acf_extract_var($el, 'label'); // validate selected if (acf_extract_var($el, 'selected')) { $el['selected'] = 'selected'; } echo acf_esc_attr($el); echo '<option ' . acf_esc_attr($el) . '>' . $label . '</option>'; } else { echo '<' . $type . ' ' . acf_esc_attr($el) . '>'; } } } echo '</select>'; } else { $els = '['; $i = 0; foreach ($field['choices'] as $k => $v) { if (is_array($v)) { $els .= '['; foreach ($v as $k2 => $v2) { foreach ($terms as $key => $val) { if ($val->name == $v2) { $els .= '["' . $v2 . '","' . $val->term_id . '",' . '"' . $slug_name[$i] . '"],'; } } } $els .= '],'; } $i++; } $els .= ']'; echo '<div class="h">'; echo '<div class="Taxonomies">'; echo '<label class="" for="' . $field['key'] . '">Taxonomies</label> '; echo '<select class="js-multi-taxonomy-select2 taxonomiesF" name="' . $field['name'] . '" id="' . $field['key'] . '-taxonomies">'; $i = 0; foreach ($field['choices'] as $k => $v) { echo '<option value="' . $i++ . '">' . $k . '</option>'; } echo '</select>'; echo '</div>'; echo '<div class="Terms">'; echo '<label class="" >Terms</label> '; echo '<input name="' . $field['name'] . '[cat]" id="' . $field['key'] . '-terms" class="js-multi-taxonomy-select2 termsF" value="" />'; echo '</div>'; echo '</div>'; // } echo ' <script> (function($){ var arr = ' . $els . '; //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ $(".TaxonomiesF").each(function(){ $(this).ready(function(){ $(this).closest(":has(.h .Terms .termsF)").find(".termsF").attr("name","' . $field['name'] . '[" + arr[$(this).closest(":has(.h .Taxonomies .TaxonomiesF)").find(".TaxonomiesF").val()][0][2]+ "]") $(this).closest(":has(.h .Terms .termsF)").find(".termsF").select2({ multiple: true, query: function (query) { var data = {results: []}, i; for (i in arr[$(this).closest(":has(.h .Taxonomies .TaxonomiesF)").find(".TaxonomiesF").val()] ) { data.results.push({id: arr[$(this).closest(":has(.h .Taxonomies .TaxonomiesF)").find(".TaxonomiesF").val()][i][1] , text: arr[$(this).closest(":has(.h .Taxonomies .TaxonomiesF)").find(".TaxonomiesF").val()][i][0]}); } query.callback(data); }, initSelection : function (element, callback) { var elementText = $(element).val(); var data = {id: elementText, text: elementText}; callback(data); } }); }); }); //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ })(jQuery); </script> '; } }
function acf_get_taxonomy_terms($taxonomies = array()) { // force array $taxonomies = acf_get_array($taxonomies); // get pretty taxonomy names $taxonomies = acf_get_pretty_taxonomies($taxonomies); // vars $r = array(); // populate $r foreach (array_keys($taxonomies) as $taxonomy) { // vars $label = $taxonomies[$taxonomy]; $terms = get_terms($taxonomy, array('hide_empty' => false)); $is_hierarchical = is_taxonomy_hierarchical($taxonomy); // bail early i no terms if (empty($terms)) { continue; } // sort into hierachial order! if ($is_hierarchical) { $terms = _get_term_children(0, $terms, $taxonomy); } // add placeholder $r[$label] = array(); // add choices foreach ($terms as $term) { $k = "{$taxonomy}:{$term->slug}"; $r[$label][$k] = acf_get_term_title($term); } } // return return $r; }
function render_field($field) { $taxonomies = array(); $taxonomies = acf_get_array($taxonomies); $taxonomies = acf_get_pretty_taxonomies($taxonomies); $taxonomy_terms = acf_get_taxonomy_terms(); $selected_taxonomies = array(); $terms = array(); $slug_name = !empty($field['choices']) ? $field['choices'] : array_keys(acf_get_pretty_taxonomies()); if ($field['tax_type'] == 'Term') { // select terms foreach ($slug_name as $k1 => $v1) { $terms = array_merge($terms, get_terms($v1, array('hide_empty' => false))); foreach ($taxonomies as $k2 => $v2) { if ($v1 == $k2) { $field['choices'][$k1] = $v2; } } } foreach ($field['choices'] as $k1 => $v1) { foreach ($taxonomy_terms as $k2 => $v2) { if ($v1 == $k2) { $selected_taxonomies[$v1] = $taxonomy_terms[$k2]; } } } } else { //select taxonomies $taxonomies = array(); foreach ($slug_name as $tax_name) { // only use allowed taxonomies $taxonomies[$tax_name] = get_taxonomy($tax_name); } foreach ($taxonomies as $taxonomy) { $selected_taxonomies[$taxonomy->name] = $taxonomy->label; } } $field['choices'] = $selected_taxonomies; // add empty value (allows '' to be selected) if (empty($field['value'])) { $field['value'] = ''; $field['value']['cat'] = ''; } // placeholder if (empty($field['placeholder'])) { $field['placeholder'] = __("Select", 'acf'); } // vars $atts = array('id' => $field['id'], 'class' => $field['class'] . ' js-multi-taxonomy-select2', 'name' => $field['name'], 'data-ui' => $field['ui'], 'data-ajax' => $field['ajax'], 'data-placeholder' => $field['placeholder'], 'data-allow_null' => $field['allow_null']); // hidden input if ($field['ui']) { acf_hidden_input(array('type' => 'hidden', 'id' => $field['id'], 'name' => $field['name'], 'value' => implode(',', $field['value']))); } elseif ($field['multiple']) { acf_hidden_input(array('type' => 'hidden', 'name' => $field['name'])); } // ui if ($field['ui']) { $atts['disabled'] = 'disabled'; $atts['class'] .= ' acf-hidden'; } // special atts foreach (array('readonly', 'disabled') as $k) { if (!empty($field[$k])) { $atts[$k] = $k; } } // vars $els = array(); $choices = array(); // loop through values and add them as options if (!empty($field['choices'])) { foreach ($field['choices'] as $k => $v) { // allowed taxonomies if (is_array($v)) { // optgroup $els[] = array('type' => 'optgroup', 'label' => $k); if (!empty($v)) { foreach ($v as $k2 => $v2) { $strip_v2_hyphen = preg_replace('#-\\s?#', '', $v2); // Child categories have hyphens before the name, we need to remove them in order to match them if ($field['type_value']) { // value = term ID foreach ($terms as $key => $val) { if ($val->name == $strip_v2_hyphen) { $els[] = array('type' => 'option', 'value' => $val->term_id, 'label' => $v2, 'selected' => $slct = $val->term_id == $field['value'] ? "selected" : ""); } } } else { // value = term slug preg_match('#(?::)(.*)#', $k2, $value); // originally returns 'taxonomy:term-slug' this removes 'taxonomy:' $els[] = array('type' => 'option', 'value' => $value[1], 'label' => $v2, 'selected' => $slct = $value[1] == $field['value'] ? "selected" : ""); } $choices[] = $k2; } } $els[] = array('type' => '/optgroup'); } else { // value = Taxonomy Slug $els[] = array('type' => 'option', 'value' => $k, 'label' => $v, 'selected' => $slct = $k == $field['value'] ? "selected" : ""); $choices[] = $k; } } } // null if ($field['allow_null']) { array_unshift($els, array('type' => 'option', 'value' => '', 'label' => '- ' . $field['placeholder'] . ' -')); } // html echo '<select ' . acf_esc_attr($atts) . '>'; // construct html if (!empty($els)) { foreach ($els as $el) { // extract type $type = acf_extract_var($el, 'type'); if ($type == 'option') { // get label $label = acf_extract_var($el, 'label'); // validate selected if (acf_extract_var($el, 'selected')) { $el['selected'] = 'selected'; } echo acf_esc_attr($el); echo '<option ' . acf_esc_attr($el) . '>' . $label . '</option>'; } else { echo '<' . $type . ' ' . acf_esc_attr($el) . '>'; } } } echo '</select>'; }