Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class MyMetaBox {
function __construct(){
add_filter('rwmb_meta_boxes', array(&$this, 'my_meta_box'));
}
public function my_meta_box($meta_boxes){
$select_advanced_ajax_id = "_my_select";
$options = [];
if(is_admin() && isset($_GET['post']) && get_post_type($_GET['post']) == 'page'):
$my_options = $this->get_my_options();
foreach($my_options as $option):
$options[$option['id']] = $option['text'];
endforeach;
endif;
$meta_boxes[] = array(
'title' => 'Infos',
'post_types' => array( 'page' ),
'fields' => array(
array(
// Real field
'name' => 'My Super Select',
'id' => $select_advanced_ajax_id,
'type' => 'select_advanced',
'options' => $options,
'sanitize_callback' => 'none',
'js_options' => array (
'minimumInputLength' => 0,
'allowClear' => true,
'placeholder' => 'Select an option',
),
),
),
);
return $meta_boxes;
}
public function get_my_options(){
$results = [];
for($i = 1; $i <= 10; $i++):
$results[] = array(
'id' => $i,
'text' => 'Option '.$i,
);
endfor;
return $results;
}
}
new MyMetaBox();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment