Skip to content

Instantly share code, notes, and snippets.

@jrfoell
Last active July 15, 2019 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrfoell/cfa2572fd3933c9242c22ddad5b4ac3b to your computer and use it in GitHub Desktop.
Save jrfoell/cfa2572fd3933c9242c22ddad5b4ac3b to your computer and use it in GitHub Desktop.
Revised
<?php
add_action( 'init', function() {
if ( class_exists( 'AcfFieldMultipleCategories' ) || ! class_exists( 'acf_field_taxonomy' ) ) {
return;
}
/**
* Class for ACF field.
*/
class AcfFieldMultipleCategories extends acf_field_taxonomy {
/**
* This function will setup the field type data.
*
* @type function
* @date 5/03/2014
* @since 5.0.0
*/
function initialize() {
parent::initialize();
// Vars.
$this->name = 'multiple_taxonomies';
$this->label = __( 'Multiple Taxonomies' );
$this->defaults = array(
'taxonomies' => array( 'category' ),
'field_type' => 'select',
'multiple' => 0,
'allow_null' => 0,
'return_format' => 'id',
'add_term' => 0,
'load_terms' => 0,
'save_terms' => 0,
'ui' => 1,
'ajax' => 1,
'ajax_action' => 'acf/fields/multiple_taxonomies/query',
'placeholder' => 'Select Term',
);
// ajax
add_action( 'wp_ajax_acf/fields/multiple_taxonomies/query', array( $this, 'ajax_query' ) );
add_action( 'wp_ajax_nopriv_acf/fields/multiple_taxonomies/query', array( $this, 'ajax_query' ) );
// Extra.
add_filter( 'acf/field_wrapper_attributes', array( $this, 'wrapper_attributes' ), 10, 2 );
}
public function wrapper_attributes( $wrapper, $field ) {
if ( 'multiple_taxonomies' === $field['type'] ) {
$wrapper['class'] .= ' acf-field-taxonomy';
$wrapper['data-type'] = 'taxonomy';
}
return $wrapper;
}
/**
* This function will return an array of data formatted for use in a select2 AJAX response.
*
* @type function
* @date 15/10/2014
* @since 5.0.9
*
* @param array $options Options.
* @return array $r Choices.
*/
function get_ajax_query( $options = array() ) {
// Defaults.
$options = acf_parse_args( $options, array(
'post_id' => 0,
's' => '',
'field_key' => '',
'paged' => 0,
) );
// Load field.
$field = acf_get_field( $options['field_key'] );
if ( ! $field ) {
return false;
}
// Vars.
$results = array();
$args = array();
$limit = 20;
$offset = 20 * ( $options['paged'] - 1 );
// Hide Empty.
$args['hide_empty'] = false;
$args['number'] = $limit;
$args['offset'] = $offset;
// Pagination
// Don't bother for hierarchial terms, we will need to load all terms anyway.
if ( $options['s'] ) {
$args['search'] = $options['s'];
}
// Get terms.
$terms = get_terms( $field['taxonomies'], $args );
/// append to r
foreach( $terms as $term ) {
// add to json
$results[] = array(
'id' => $term->term_id,
'text' => $this->get_term_title( $term, $field, $options['post_id'] )
);
}
// vars
$response = array(
'results' => $results,
'limit' => $limit
);
// Return.
return $response;
}
/**
* This filter is appied to the $value after it is loaded from the db.
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param string $value The value found in the database.
* @param int $post_id The post ID from which the value was loaded from.
* @param array $field The field array holding all the field options.
*
* @return $value - the value to be saved in te database
*/
function load_value( $value, $post_id, $field ) {
// Return.
return $value;
}
/**
* Create the HTML interface for your field.
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param array $field An array holding all the field's data.
*/
function render_field( $field ) {
// Force value to arra.
$field['value'] = acf_get_array( $field['value'] );
$field['multiple'] = 0;
// Vars.
$div = array(
'class' => 'acf-taxonomy-field acf-soh',
'data-save' => $field['save_terms'],
'data-type' => $field['field_type'],
'data-taxonomies' => $field['taxonomies'],
'data-ftype' => 'select',
);
?>
<div <?php acf_esc_attr_e( $div ); ?>>
<?php $this->render_field_select( $field ); ?>
</div>
<?php
}
/**
* Create the HTML interface for your field/
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param array $field An array holding all the field's data.
*/
function render_field_select( $field ) {
// Change Field into a select.
$field['type'] = 'select';
$field['ui'] = 1;
$field['ajax'] = 1;
$field['choices'] = array();
$choices = array();
if ( count( $field['value'] ) >= 1 ) {
$term = get_term_by_id( $field['value'][0] );
$choices[ $field['value'][0] ] = $term->name;
}
$field['choices'] = $choices;
acf_render_field( $field );
}
/**
* Create extra options for your field. This is rendered when editing a field.
* The value of $field['name'] can be used (like bellow) to save extra data to the $field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param array $field An array holding all the field's data.
*/
function render_field_settings( $field ) {
$taxes = acf_get_taxonomies();
// Default value.
acf_render_field_setting( $field, array(
'label' => __( 'Taxonomies' ),
'instructions' => __( 'Select the taxonomies to be displayed' ),
'type' => 'select',
'name' => 'taxonomies',
'multiple' => 1,
'ui' => 1,
'choices' => array_combine( $taxes, $taxes ),
) );
// Allow null.
acf_render_field_setting( $field, array(
'label' => __( 'Allow Null?', 'acf' ),
'instructions' => '',
'name' => 'allow_null',
'type' => 'true_false',
'ui' => 1,
'conditions' => array(
'field' => 'field_type',
'operator' => '!=',
'value' => 'checkbox'
)
) );
// Save terms.
acf_render_field_setting( $field, array(
'label' => __( 'Save Terms', 'acf' ),
'instructions' => __( 'Connect selected terms to the post', 'acf' ),
'type' => 'radio',
'name' => 'save_terms',
'type' => 'true_false',
'ui' => 1,
) );
// Load terms.
acf_render_field_setting( $field, array(
'label' => __( 'Load Terms', 'acf' ),
'instructions' => __( 'Load value from posts terms', 'acf' ),
'name' => 'load_terms',
'type' => 'true_false',
'ui' => 1,
) );
// Return format.
acf_render_field_setting( $field, array(
'label' => __( 'Return Value', 'acf' ),
'instructions' => '',
'type' => 'radio',
'name' => 'return_format',
'choices' => array(
'object' => __( 'Term Object', 'acf' ),
'id' => __( 'Term ID', 'acf' ),
),
'layout' => 'horizontal',
) );
}
}
acf_register_field_type( 'AcfFieldMultipleCategories' );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment