I have successfully added the custom taxonomy for employer tags to the employer table. However, I am currently unable to retrieve and save the values from the input field on the meta box. As of now, tags can only be added manually through the WordPress dashboard. I attempted the following code to resolve this issue, but it did not work as expected.
this is the input:
<input type="text" class="regular-text" name="tag" id="tag" value="" placeholder="tag1, tag2, tag3"'/>
function create_employer_custom_taxonomy() {
$labels = array(
'name' => _x('Employer Tags', 'taxonomy general name', 'textdomain'),
'singular_name' => _x('Employer Tag', 'taxonomy singular name', 'textdomain'),
'search_items' => __('Search Employer Tags', 'textdomain'),
'all_items' => __('All Employer Tags', 'textdomain'),
'parent_item' => __('Parent Employer Tag', 'textdomain'),
'parent_item_colon' => __('Parent Employer Tag:', 'textdomain'),
'edit_item' => __('Edit Employer Tag', 'textdomain'),
'update_item' => __('Update Employer Tag', 'textdomain'),
'add_new_item' => __('Add New Employer Tag', 'textdomain'),
'new_item_name' => __('New Employer Tag Name', 'textdomain'),
'menu_name' => __('Employer Tags', 'textdomain'),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array('slug' => 'employer_tag'),
);
register_taxonomy('employer_tag', 'employer', $args);
}
add_action('init', 'create_employer_custom_taxonomy');
function add_employer_tags_meta_box() {
add_meta_box(
'employer_tags_meta_box',
'Employer Tags',
'employer_tags_meta_box_callback',
'employer', // Post type
'normal',
'high'
);
}
add_action('add_meta_boxes', 'add_employer_tags_meta_box');
function employer_tags_meta_box_callback($post) {
wp_nonce_field('save_employer_tags_meta_box_data', 'employer_tags_meta_box_nonce');
$tags = wp_get_post_terms($post->ID, 'employer_tag', array('fields' => 'names'));
$tags_string = implode(', ', $tags); // Convert array to comma-separated string
echo '<div class="cmb-td">';
echo '<input type="text" class="regular-text" name="tag" id="tag" value="' . esc_attr($tags_string) . '" placeholder="Tag1, Tag2, Tag3" />';
echo '</div>';
}
function save_employer_tags_meta_box_data($post_id) {
if (!isset($_POST['employer_tags_meta_box_nonce'])) {
return;
}
if (!wp_verify_nonce($_POST['employer_tags_meta_box_nonce'], 'save_employer_tags_meta_box_data')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (isset($_POST['tag'])) {
$tags = sanitize_text_field($_POST['tag']);
// Split tags by comma and trim whitespace
$tags_array = array_map('trim', explode(',', $tags));
// Save tags to the custom taxonomy
if (!empty($tags_array)) {
foreach ($tags_array as $tag) {
if (!term_exists($tag, 'employer_tag')) {
wp_insert_term($tag, 'employer_tag');
}
}
wp_set_post_terms($post_id, $tags_array, 'employer_tag', false);
}
}
}
add_action('save_post', 'save_employer_tags_meta_box_data');
function display_employer_tags() {
global $post;
$tags = wp_get_post_terms($post->ID, 'employer_tag');
if (!empty($tags)) {
echo '<div class="employer-tags">';
echo '<h3>Tags:</h3>';
echo '<ul>';
foreach ($tags as $tag) {
echo '<li>' . esc_html($tag->name) . '</li>';
}
echo '</ul>';
echo '</div>';
}
}
add_action('freeio_single_employer_after', 'display_employer_tags');