Created
February 25, 2021 12:39
-
-
Save harkor/2fad09a642a096eb0a48984669211312 to your computer and use it in GitHub Desktop.
MetaBox Select Advanced Ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MetaBoxClass { | |
function __construct(){ | |
// Set MetaBox | |
add_filter('rwmb_meta_boxes', array(&$this, 'my_meta_box')); | |
// Add new field type | |
add_action('init', array(&$this, 'prefix_load_SelectAdvancedAjax_type')); | |
// Ajax call | |
add_action('wp_ajax_get_my_options', array(&$this, 'get_my_options')); | |
add_action('wp_ajax_nopriv_get_my_options', array(&$this, 'get_my_options')); | |
} | |
public function my_meta_box($meta_boxes){ | |
$select_advanced_ajax_id = "_my_select"; | |
$meta_boxes[] = array( | |
'title' => 'Infos', | |
'post_types' => array( 'page' ), | |
'fields' => array( | |
array( | |
'name' => 'My Super Select', | |
'id' => "_my_super_select", | |
'type' => 'select_advanced_ajax', | |
'sanitize_callback' => 'none', | |
'js_options' => array ( | |
'ajax' => array( | |
'url' => '/wp-admin/admin-ajax.php', | |
'dataType' => 'json', | |
'type' => 'post', | |
'data' => [ | |
'action' => 'get_my_options', | |
], | |
'delay' => '250', | |
), | |
'minimumInputLength' => 0, | |
'allowClear' => true, | |
'placeholder' => 'Select an option', | |
), | |
), | |
), | |
); | |
return $meta_boxes; | |
} | |
public function get_my_options(){ | |
$results = [ | |
'results' => [], | |
]; | |
for($i = 1; $i <= 10; $i++): | |
$results['results'][] = array( | |
'id' => $i, | |
'text' => 'Option '.$i, | |
); | |
endfor; | |
wp_send_json($results); | |
wp_die(); | |
} | |
public function prefix_load_SelectAdvancedAjax_type(){ | |
require 'RWMB_Select_advanced_ajaxField.php'; | |
} | |
} | |
new MyMetaBox(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RWMB_Select_advanced_ajax_Field extends RWMB_Select_Advanced_Field { | |
public static function html( $meta, $field ) { | |
$options = self::transform_options( $field['options'] ); | |
$attributes = self::call( 'get_attributes', $field, $meta ); | |
$attributes['data-selected'] = $meta; | |
$walker = new RWMB_Walker_Select( $field, $meta ); | |
$attributes['class'] .= ' rwmb-select_advanced'; | |
$output = sprintf( | |
'<select %s>', | |
self::render_attributes( $attributes ) | |
); | |
if ( ! $field['multiple'] && $field['placeholder'] ) { | |
$output .= '<option value="">' . esc_html( $field['placeholder'] ) . '</option>'; | |
} | |
$output .= $walker->walk( $options, $field['flatten'] ? -1 : 0 ); | |
$output .= '</select>'; | |
$output .= self::get_select_all_html( $field ); | |
$interfaceValue = rwmb_meta($field['id'] .'-interface'); | |
// Hidden field for remember the content of the selector | |
$output .= '<input | |
type="hidden" | |
id="'. $field['id'] .'-interface" | |
name="'. $field['id'] .'-interface" | |
value=\''. $interfaceValue .'\' />'; | |
// JavaScript to handle the select (see select2 documentation) | |
$output .= '<script type="text/javascript"> | |
jQuery(function($){ | |
var $interface = $("#'. $field['id'] .'-interface"); | |
var $field = jQuery("#'. $field['id'] .'"); | |
$field.on("change", function(){ | |
var $this = $(this); | |
var value = $this.find(":selected").val(); | |
var text = $this.find(":selected").text(); | |
var data = ""; | |
if(value != ""){ | |
data = JSON.stringify({ value : value, text : text }); | |
} | |
$interface.val(data); | |
}); | |
if($interface.val() != ""){ | |
var data = JSON.parse($interface.val()); | |
var newOption = new Option(data.text, data.value, true, true); | |
$field.append(newOption); | |
} | |
}); | |
</script>'; | |
return $output; | |
} | |
public static function save( $new, $old, $post_id, $field ) { | |
// Force saving our hidden field | |
update_metadata( 'post', $post_id, $field['id'].'-interface', $_POST[$field['id'].'-interface']); | |
parent::save($new, $old, $post_id, $field); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment