Skip to content

Instantly share code, notes, and snippets.

@harkor
Created February 25, 2021 10:57
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 harkor/e66dff210997fe552ec6501b35c8568c to your computer and use it in GitHub Desktop.
Save harkor/e66dff210997fe552ec6501b35c8568c to your computer and use it in GitHub Desktop.
<?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