Skip to content

Instantly share code, notes, and snippets.

@harkor
Created Feb 25, 2021
Embed
What would you like to do?
<?php
class MyMetaBox {
function __construct(){
add_filter('rwmb_meta_boxes', array(&$this, 'my_meta_box'));
add_action('wp_ajax_get_options', array(&$this, 'get_options'));
}
public function my_meta_box($meta_boxes){
$meta_boxes[] = array(
'title' => 'Infos',
'post_types' => array( 'page' ),
'fields' => array(
array(
'name' => 'My Super Select',
'id' => '_my_select',
'type' => 'select_advanced',
'sanitize_callback' => 'none',
'js_options' => array (
'ajax' => array(
'url' => '/wp-admin/admin-ajax.php',
'dataType' => 'json',
'type' => 'post',
'data' => [
'action' => 'get_options',
],
'delay' => '250',
),
'minimumInputLength' => 0,
'allowClear' => true,
'placeholder' => 'Select an option',
),
),
)
);
return $meta_boxes;
}
public function get_options(){
$results = [
'results' => [],
];
for($i = 1; $i <= 10; $i++):
$results['results'][] = array(
'id' => $i,
'text' => 'Option '.$i,
);
endfor;
wp_send_json($results);
wp_die();
}
}
new MyMetaBox();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment