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