├── README.md ├── acf-autocomplete-v4.php ├── acf-autocomplete.php ├── acf-field-type-autocomplete.php ├── css └── input.css └── js └── input.js /README.md: -------------------------------------------------------------------------------- 1 | === Advanced Custom Fields: Autocomplete Field === 2 | 3 | Contributors: Brian S. Reed, Max Lyuchin 4 | Requires at least: 3.5 5 | Tested up to: 3.8.1 6 | Stable tag: trunk 7 | License: GPLv2 or later 8 | License URI: http://www.gnu.org/licenses/gpl-2.0.html 9 | 10 | Simple field that looks up values previously entered for this field. 11 | 12 | == Description == 13 | 14 | This is a simple field that looks up values previously entered for this field. This plugin is great for posts that will have a common set of values for the same field but where taxonomy isn't applicable. Plugin uses WordPress core's jQuery UI Autocomplete. 15 | 16 | = Compatibility = 17 | 18 | This ACF field type is compatible with: 19 | * ACF 5 20 | * ACF 4 21 | 22 | == Installation == 23 | 24 | 1. Copy the `acf-field-type-autocomplete` folder into your `wp-content/plugins` folder 25 | 2. Activate the Advanced Custom Fields: Autocomplete plugin via the plugins admin page 26 | 3. Create a new field via ACF and select the Autocomplete type 27 | 4. Please refer to the description for more info regarding the field type settings 28 | 29 | == Changelog == 30 | 31 | = 1.0.0 = 32 | * Initial Release. 33 | -------------------------------------------------------------------------------- /acf-autocomplete-v4.php: -------------------------------------------------------------------------------- 1 | name = 'autocomplete'; 15 | $this->label = __('Autocomplete'); 16 | $this->category = __("Basic",'acf'); // Basic, Content, Choice, etc 17 | $this->defaults = array(); 18 | 19 | // do not delete! 20 | parent::__construct(); 21 | 22 | add_action( 'wp_ajax_autocomplete_ajax', array( $this, 'autocomplete_ajax_callback' ) ); 23 | 24 | } 25 | 26 | 27 | public function autocomplete_ajax_callback() { 28 | 29 | global $wpdb; 30 | 31 | $results = array(); 32 | 33 | $results = $wpdb->get_col( $wpdb->prepare( " 34 | SELECT DISTINCT meta_value FROM {$wpdb->postmeta} 35 | WHERE meta_key = '%s' 36 | AND meta_value LIKE %s 37 | ", $_REQUEST['field_key'], '%'.$_REQUEST['request'].'%' ) ); 38 | 39 | echo json_encode($results); 40 | 41 | wp_die(); 42 | 43 | } 44 | 45 | 46 | function create_field( $field ) 47 | { 48 | 49 | ?> 50 | 51 | 83 | -------------------------------------------------------------------------------- /acf-autocomplete.php: -------------------------------------------------------------------------------- 1 | name = 'autocomplete'; 8 | 9 | $this->label = __('Autocomplete', 'acf-autocomplete'); 10 | 11 | $this->category = 'basic'; 12 | 13 | $this->defaults = array( 14 | 'font_size' => 14, 15 | ); 16 | 17 | $this->l10n = array( 18 | 'error' => __('Error! Please enter a higher value', 'acf-autocomplete'), 19 | ); 20 | 21 | parent::__construct(); 22 | 23 | add_action( 'wp_ajax_autocomplete_ajax', array( $this, 'autocomplete_ajax_callback' ) ); 24 | 25 | } 26 | 27 | public function autocomplete_ajax_callback() { 28 | 29 | global $wpdb; 30 | 31 | $results = array(); 32 | 33 | //$results = array( 'post' => $_POST, 'get' => $_GET ); 34 | 35 | $results = $wpdb->get_col( $wpdb->prepare( " 36 | SELECT DISTINCT postmeta2.meta_value FROM $wpdb->postmeta as postmeta1, $wpdb->postmeta as postmeta2 WHERE postmeta1.meta_value = '%s' AND postmeta1.meta_key = CONCAT( '_', postmeta2.meta_key ) AND postmeta2.meta_value LIKE %s", 37 | $_REQUEST['field_key'], 38 | '%' . $_REQUEST['request'] . '%' 39 | ) ); 40 | 41 | echo json_encode($results); 42 | 43 | 44 | wp_die(); 45 | 46 | } 47 | 48 | function render_field_settings( $field ) { 49 | 50 | acf_render_field_setting( $field, array( 51 | 'label' => __('Font Size','acf-autocomplete'), 52 | 'instructions' => __('Customise the input font size','acf-autocomplete'), 53 | 'type' => 'number', 54 | 'name' => 'font_size', 55 | 'prepend' => 'px', 56 | )); 57 | 58 | } 59 | 60 | function render_field( $field ) { 61 | 62 | ?> 63 | 64 | 81 | -------------------------------------------------------------------------------- /acf-field-type-autocomplete.php: -------------------------------------------------------------------------------- 1 | 36 | -------------------------------------------------------------------------------- /css/input.css: -------------------------------------------------------------------------------- 1 | ul.ui-autocomplete li { cursor: pointer; } 2 | ul.ui-autocomplete li:hover { background: #ccc; } 3 | -------------------------------------------------------------------------------- /js/input.js: -------------------------------------------------------------------------------- 1 | (function($){ 2 | 3 | 4 | function initialize_field( $el ) { 5 | 6 | $(':text:visible',$el).autocomplete({ 7 | source: function( request, response ){ 8 | 9 | if(!request.term.trim().length) 10 | response( [] ); 11 | 12 | $.getJSON( ajaxurl, { 13 | 'action' : 'autocomplete_ajax', 14 | 'field_key' : $el.data('field_name'), 15 | 'request' : request.term.trim() 16 | }, function( data ){ 17 | 18 | response( data ); 19 | 20 | }); 21 | } 22 | }); 23 | } 24 | 25 | if( typeof acf.add_action !== 'undefined' ) { 26 | 27 | /* 28 | * ready append (ACF5) 29 | */ 30 | 31 | acf.add_action('ready append', function( $el ){ 32 | 33 | // search $el for fields of type 'autocomplete' 34 | acf.get_fields({ type : 'autocomplete'}, $el).each(function(){ 35 | 36 | initialize_field( $(this) ); 37 | 38 | }); 39 | 40 | }); 41 | 42 | 43 | } else { 44 | 45 | 46 | /* 47 | * acf/setup_fields (ACF4) 48 | */ 49 | 50 | $(document).on('acf/setup_fields', function(e, postbox){ 51 | 52 | $(postbox).find('.field[data-field_type="autocomplete"]').each(function(){ 53 | 54 | initialize_field( $(this) ); 55 | 56 | }); 57 | 58 | }); 59 | 60 | 61 | } 62 | 63 | 64 | })(jQuery); --------------------------------------------------------------------------------