Last active
December 30, 2015 04:09
-
-
Save chriskoelle/7774060 to your computer and use it in GitHub Desktop.
Custom Meta Box - Custom Meta Types - for use with Jared Atchison's Custom Metaboxes and Fields for WordPresshttps://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress
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 | |
// Custom Post Type Dropdown | |
function cmb_post_type_dropdown($field, $meta) { | |
wp_dropdown_pages(array( | |
'show_option_none' => '-- Select One ---', | |
'name' => $field['id'], | |
'id' => $field['id'], | |
'post_type' => $field['post_type'], | |
'selected' => $meta | |
)); | |
} | |
add_filter( 'cmb_render_post_type_dropdown', 'cmb_post_type_dropdown', 10, 2 ); | |
// Custom Post Type Checklist | |
function cmb_post_type_checklist($field, $meta) { | |
if( !post_type_exists( $field['post_type'] )) return; | |
$args = array( | |
'post_type' => $field['post_type'], | |
'posts_per_page' => -1, | |
'orderby' => 'title', | |
'order' => 'ASC' | |
); | |
if(isset($field['taxonomy']) && taxonomy_exists($field['taxonomy']) && isset($field['terms']) && is_array($field['terms']) ): | |
$args['tax_query'] = array( | |
array( | |
'taxonomy' => $field['taxonomy'], | |
'field' => 'slug', | |
'terms' => $field['terms'] | |
) | |
); | |
endif; | |
$checks = new WP_Query($args); | |
if ( $checks->have_posts() ) : | |
echo '<ul class="post-type-checklist">'; | |
while ( $checks->have_posts() ) : $checks->the_post(); | |
$checked = in_array(get_the_ID(), (array) $meta) ? 'checked' : ''; | |
printf('<li><input type="checkbox" value="%1$s" id="page-checkbox-%1$s" name="%2$s[]" %3$s><label for="page-checkbox-%1$s">%4$s</label></li>', get_the_ID(), $field['id'], $checked, get_the_title() ); | |
endwhile; | |
echo '</ul>'; | |
endif; | |
} | |
add_filter( 'cmb_render_post_type_checklist', 'cmb_post_type_checklist', 10, 2 ); | |
/** | |
* Post search (by title) field with autocomplete. | |
* Saves the post ID as the meta value. | |
*/ | |
function cmb_post_autocomplete($field, $meta) { | |
wp_enqueue_script( 'jquery-ui-autocomplete' ); | |
$post_title = is_numeric($meta) ? get_the_title( intval($meta) ) : ''; | |
$post_type = (isset($field['args']['post_type']) && post_type_exists($field['args']['post_type'])) ? $field['args']['post_type'] : 'post'; | |
$q = new WP_Query(array( | |
'post_type' => $post_type, | |
'post_status' => 'publish', | |
'posts_per_page' => -1 | |
)); | |
$data = array(); | |
if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); | |
$data[] = array( | |
'post_id' => get_the_ID(), | |
'value' => get_the_title() | |
); | |
endwhile; endif; | |
wp_reset_postdata(); | |
?> | |
<div class="cmb-autocomplete-set"> | |
<input class="cmbap-hidden" type="hidden" name="<?php echo $field['id']; ?>" id="<?php echo $field['id']; ?>" value="<?php echo intval($meta); ?>"> | |
<input class="cmbap-text" type="text" value="<?php esc_attr_e($post_title); ?>" id="<?php echo $field['id']; ?>-title"> | |
</div> | |
<script> | |
jQuery(function($) { | |
var ac_terms = <?php echo json_encode($data); ?>; | |
var $title_field = $("#<?php echo $field['id']; ?>-title"); | |
var $id_field = $("#<?php echo $field['id']; ?>"); | |
$title_field.autocomplete({ | |
source: function(request, response) { | |
var results = $.ui.autocomplete.filter(ac_terms, request.term); | |
response(results.slice(0, 10)); | |
}, | |
select: function(event, ui) { | |
$id_field.val(ui.item.post_id); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} | |
add_filter( 'cmb_render_post_autocomplete', 'cmb_post_autocomplete', 10, 2 ); | |
function cmb_validate_post_autocomplete($override_value, $value, $object_id, $field_args) { | |
$post_type = (isset($field_args['post_type']) && post_type_exists( $field_args['post_type'])) ? $field_args['post_type'] : 'post'; | |
if( is_numeric($value) && get_post_type( $post_id ) === $post_type): | |
$override_value = $value; | |
endif; | |
return $override_value; | |
} | |
add_filter( 'cmb_validate_post_autocomplete', 'cmb_validate_post_autocomplete', 10, 4 ); |
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 | |
function example_meta_boxes( ar ray $meta_boxes ) { | |
$prefix = '_cmb_'; | |
$meta_boxes[] = array( | |
'id' => 'test_meta', | |
'title' => 'Example', | |
'pages' => array( 'page',, // Post type | |
'context' => 'normal', | |
'priority' => 'high', | |
'show_names' => true, // Show field names on the left | |
'fields' => array( | |
array( | |
'name' => 'Page Checklist', | |
'id' => $prefix . 'page_checklist', | |
'type' => 'post_type_checklist', | |
'post_type' => 'post', | |
'taxonomy' => 'category', | |
'terms' => array('cats') | |
), | |
array( | |
'name' => 'Page Dropdown', | |
'id' => $prefix .'page_dropdown', | |
'type' => 'post_type_dropdown', | |
'post_type' => 'page' | |
), | |
array( | |
'name' => 'Post Type Autocomplete ID', | |
'id' => $prefix . 'pt_autocomplete', | |
'type' => 'post_autocomplete', | |
'args' => array( | |
'post_type' => 'custom_post_type', | |
) | |
), | |
), | |
); | |
return $meta_boxes; | |
} | |
add_filter( 'cmb_meta_boxes', 'example_meta_boxes' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment