Save a Custom Meta Boxes plugin Taxonomy Select Field as a term for a post - Cobbled Code

The helpful Custom Meta Boxes plugin by Human Made provides a Taxonomy Select Field. Ever so sneakily, instead of adding the post to the selected term as one might expect, the plugin saves the taxonomy term id in a custom field. Let’s fix that.

For things to actually work, the Custom Meta Boxes plugin must be installed and activated. The code from this example goes into your theme’s functions.php file or better yet, inside your own custom plugin.

First, add a filter which will register our meta box:

// Register a meta box via the Custom Meta Boxes plugin.
add_filter( 'cmb_meta_boxes', 'prefix_meta_box_category' );

The CMB plugin allows you to use your own function to save the selected value of a field. This function is registered by passing the name of the function via the field’s save_callback option.

We’re going to use that function to assign the post to a category instead of the default behaviour of saving the term_id of the selected term in a custom field.

The callback function that will save the post category:

/**
 * Callback for saving the 'category' meta box field value.
 *
 * @see prefix_meta_box_category()
 * 
 * @param  array $values Taxonomy select value. 
 * @param  array $post_id Post ID.
 */
function prefix_save_post_category( $values, $post_id ) {

	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return;
	}
	
	wp_set_object_terms(
		$post_id,
		absint( $values['cmb-field-0'] ),
		'category',
		false
	);
}

The final step is to add the function that will register the fields used in the meta box.

Note that the plugin, when displaying the category select field, will not know which category the post belongs to. It still expects the post category to be saved in a custom field.

To work around this we’ll use the select field’s default option and set it to the term_id of the selected category.

/**
 * Register category select cmb meta box.
 *
 * Adds a CMB plugin meta box to select a term in the 'category' taxonomy.
 *
 * @see prefix_save_post_category()
 *
 * @param  array $meta_boxes List of registered meta boxes.
 * @return array Filtered list of registered meta boxes.
 */
function prefix_meta_box_category( array $meta_boxes ) {

	$default = 0;

	// The ID of the post we're editing.
	if ( ! empty( $_GET['post'] ) ) {
		
		// Get the terms in the 'category' taxonomy assigned to the post .
		$terms = get_the_terms( absint( $_GET['post'] ), 'category' );
		
		if ( $terms && ! is_wp_error( $terms ) ) {
			
			// Default to the first term in the array.
			$default = $terms[0]->term_id;
		}
	}

	$fields = array(
		array(
			'id'   => 'category',
			'name' => '',
			'type' => 'taxonomy_select',
			'taxonomy' => 'category',
			'allow_none' => true,
			'default' => $default,
			// This holds the name of our callback function.
			'save_callback' => 'prefix_save_post_category',
		)
	);

	$meta_boxes[] = array(
		'title' => esc_html__( 'Category', 'textdomain' ),
		'pages' => array( 'post' ),
		'context' => 'normal',
		'priority' => 'high',
		'fields' => $fields,
	);

	return $meta_boxes;
}

Voilá! Now, adding a dropdown for adding a post to a standard WordPress category may not be all that useful. The same procedure however could come in handy when working with your own custom taxonomy. Sometimes using a custom meta box to assign a post to a taxonomy fits a project’s requirements better than using the default WordPress interface.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.