Last active
January 21, 2022 21:17
-
-
Save regiside/9e33a036c79e0455fdca1ed1526048de to your computer and use it in GitHub Desktop.
Allow Select2 taggable input on custom taxonomy metabox for Wordpress
This file contains hidden or 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 | |
// I wanted to be able to use Select2's tagging feature to add new terms to posts in wordpress. I slighlty modified the code from Misha Rudrastyh's excelent tutorial(link below). Comments are closed on that post so I figured I'd post this gist for anyone else wanting to do this. Here's the portion of code I modified: | |
// @https://rudrastyh.com/wordpress/select2-for-metaboxes-with-ajax.html | |
// Note: if you'd like to limit selection you can remove "multiple="multiple"" from the select box, | |
//or you can use Select2's "maximumSelectionLength" feature to limit to any number. | |
function rudr_display_select2_metabox( $post_object ) { | |
// do not forget about WP Nonces for security purposes | |
// I decided to write all the metabox html into a variable and then echo it at the end | |
$html = ''; | |
// always array because we have added [] to our <select> name attribute | |
// Create an array of the current term_ids | |
$term_ids = array(); | |
$term_objs = get_the_terms( $post_object->ID, 'your-taxonomy' ); | |
foreach ($term_objs as $term_obj){ | |
$term_ids[] = $term_obj->term_id; //Add the term_id to the array. | |
} | |
/* | |
* It will be just a multiple select for tags without AJAX search | |
* If no tags found - do not display anything | |
* hide_empty=0 means to show tags not attached to any posts | |
*/ | |
if( $tags = get_terms( 'your-taxonomy', 'hide_empty=0' ) ) { | |
$html .= '<p><label for="rudr_select2_tags">Your Taxonomy:</label><br /><select id="rudr_select2_tags" name="rudr_select2_tags[]" multiple="multiple" style="width:99%;max-width:25em;">'; | |
foreach( $tags as $tag ) { | |
$selected = ( is_array( $term_ids ) && in_array( $tag->term_id, $term_ids ) ) ? ' selected="selected"' : ''; | |
$html .= '<option value="' . $tag->name . '"' . $selected . '>' . $tag->name . '</option>'; //I used tag name instead of $tag->term_id for the value so that I could pass it to wordpress and it would handle creating a new term ID if it was a new term name. | |
} | |
$html .= '</select></p>'; | |
} | |
echo $html; | |
} | |
function rudr_save_metaboxdata( $post_id, $post ) { | |
$my-taxonomy = 'custom-taxonomy'; | |
$my-post-type = 'your-post-type'; | |
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id; | |
// if post type is different from our selected one, do nothing | |
if ( $post->post_type == $my-post-type ) { | |
if( isset( $_POST['rudr_select2_tags'] ) ){ | |
$termIDs = $_POST['rudr_select2_tags']; | |
wp_set_object_terms($post_id, $termIDs, $my-taxonomy ); | |
}else{ | |
wp_delete_object_term_relationships($post_id, $my-taxonomy ); | |
} | |
} | |
return $post_id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment