Inventory_Presser_Taxonomies::taxonomy_meta_box_html( string $taxonomy_name, string $element_name, WP_Post $post )

Creates HTML output for a meta box that turns a taxonomy into a select drop-down list instead of the typical checkboxes. Including a blank option is the only way a user can remove the value.


Parameters

$taxonomy_name

(string) (Required)

$element_name

(string) (Required)

$post

(WP_Post) (Required) A post


Return

(string) HTML that renders a editor meta box for a taxonomy


Source

File: includes/class-taxonomies.php

	protected static function taxonomy_meta_box_html( $taxonomy_name, $element_name, $post ) {
		$html = sprintf(
			'<select name="%s" id="%s"><option></option>',
			$element_name,
			$element_name
		);

		// get all the term names and slugs for $taxonomy_name.
		$terms = get_terms( $taxonomy_name, array( 'hide_empty' => false ) );

		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
			// get the saved term for this taxonomy.
			$saved_term_slug = self::get_term_slug( $taxonomy_name, $post->ID );

			foreach ( $terms as $term ) {
				$html .= sprintf(
					'<option value="%s"%s>%s</option>',
					$term->slug,
					selected( strtolower( $term->slug ), strtolower( $saved_term_slug ), false ),
					$term->name
				);
			}
		}
		return $html . '</select>';
	}