update_term_meta() only updating once on certain meta keys

I set up 4 custom meta fields on a custom taxonomy, and for some reason the field values for my 2 text fields only update the first time they are added to the database. Either from including them in the "add" form or if I create a taxonomy without them and then include them in the "edit" form for the first time. But as soon as I try to change the values, the database does not get updated. What am I doing wrong?

Here is my code:

// Add the fields to the "groups" taxonomy, using our callback function  
add_action( 'groups_add_form_fields', 'groups_taxonomy_custom_fields', 10, 2 ); // "Add new" form
add_action( 'groups_edit_form_fields', 'groups_taxonomy_custom_fields', 10, 2 ); // "Edit" form
function groups_taxonomy_custom_fields($term) {
// Get the page
global $pagenow;
// Set empty vars
$organization = '';
$email_domain = '';
$facilitators = array();
$access_ids = array();
// If we are on the "add new" page
if ($pagenow == 'edit-tags.php') {
// Table setup
$cont1 = '<div class="form-field">';
$cont2 = '';
$cont3 = '</div>';
// If we are on the "edit" term page
} elseif ($pagenow == 'term.php') {
// Check for existing taxonomy meta for the term you're editing
$term_id = isset($term->term_id) ? $term->term_id : null;
$organization = get_term_meta( $term_id, 'organization', true );
$email_domain = get_term_meta( $term_id, 'email_domain', true );
if (get_term_meta( $term_id, 'facilitators', true )) {
$facilitators = get_term_meta( $term_id, 'facilitators', true );
}
if (get_term_meta( $term_id, 'access_ids', true )) {
$access_ids = get_term_meta( $term_id, 'access_ids', true );
}
// Table setup
$cont1 = '<tr class="form-field">  
<th scope="row" valign="top">';
$cont2 = '</th>  
<td>';
$cont3 = '</td>  
</tr>';
// Else nothing
} else {
$cont1 = '';
$cont2 = '';
$cont3 = '';
}
// Let's get all the webinar codes
$codes = get_terms( array(
'taxonomy' => eri_access_id_taxonomy(),
'hide_empty' => false,
'order' => 'DESC',
) );
// Now get all the Facilitators
$users = get_users( array(
'role__in'  => array( 'facilitator' ),
'orderby'   => 'display_name',
'order'     => 'ASC'
) );
?>  
<!-- ADD SOME CSS -->
<style>
.yoast-notification {
display: none;
}
.editor-training-groups-list {
background: white;
max-height: 20em;
overflow: hidden;
padding-left: 6px;
padding-top: 6px;
width: 94%;
overflow-y:auto;
}
.editor-training-groups-choice {
margin-bottom: 10px;
float: left;
clear: both;
}
.editor-code-label {
vertical-align: top !important;
display: inline !important;
}
.training-type-title {
font-weight: bold;
float: left;
clear: both;
margin-bottom: 10px;
}
</style>
<!-- ORGANIZATION FIELD -->
<?php echo $cont1; ?>   
<label for="organization"><?php _e('Organization/Group'); ?></label>  
<?php echo $cont2 ?? $cont2; ?>  
<input type="text" name="organization" id="organization" size="25" value="<?php echo $organization; ?>"><br />  
<span class="description"><?php _e('The full name of the organization or group.'); ?></span>  
<?php echo $cont3; ?>
<!-- EMAIL DOMAIN FIELD -->
<?php echo $cont1; ?>  
<label for="email_domain"><?php _e('Email Domain'); ?></label>  
<?php echo $cont2 ?? $cont2; ?> 
<input type="text" name="email_domain" id="email_domain" size="25" value="<?php echo $email_domain; ?>"><br />  
<span class="description"><?php _e('Your organization's email domain name  (after the @ symbol) that users will mostly be using to register. (NOTE: Anybody who registers using an email address with this domain will be automatically added to this training group.)'); ?></span>  
<?php echo $cont3; ?>
<!-- FACILITATORS FIELD -->
<?php echo $cont1; ?>  
<span><?php _e('Facilitators'); ?></span>
<?php echo $cont2 ?? $cont2; ?> 
<div class="editor-training-groups-list">
<?php
// Vars
$the_users_checked = [];
$the_users = [];
// Cycle through each user
foreach ($users as $user) {
$is_checked = false;
$checked = '';
if (in_array($user->ID, $facilitators)) {
$is_checked = true;
$checked = ' checked="checked"';
}
$id = '<div class="editor-training-groups-choice">
<input type="hidden" value="0" name="facilitators['.$user->ID.']">
<input type="checkbox" id="facilitators['.$user->ID.']" name="facilitators['.$user->ID.']" value="1" '.$checked.'/><label for="facilitators['.$user->ID.']" class="editor-code-label">'.$user->display_name.' <span class="limit-length">('.$user->user_email.'): '.$user->organization.'</span></label>
</div>';
if ($is_checked) {
$the_users_checked[] = $id;
} else {
$the_users[] = $id;
}
}
echo implode('', $the_users_checked);
echo implode('', $the_users);
?>
</div>  
<span class="description"><?php _e('Select the Facilitators that will have access to this trainings dashboard.'); ?></span>  
<?php echo $cont3; ?>
<!-- TRAININGS FIELD -->
<?php echo $cont1; ?>  
<span><?php _e('Trainings'); ?></span>
<?php echo $cont2 ?? $cont2; ?> 
<div class="editor-training-groups-list">
<?php
// Let's get all of the codes to create a link
$tta_names = [];
foreach ($codes as $code) {
$code_names[] = strtolower($code->name);
}
// Create empty arrays to store them
$tta_names = [];
$ita_names = [];
$uta_names = [];
$tta_checked = [];
$ita_checked = [];
$uta_checked = [];
$tta = [];
$ita = [];
$uta = [];
// Cycle through each term
foreach ($codes as $code) {
$is_checked = false;
$checked = '';
if (in_array($code->name, $access_ids)) {
$is_checked = true;
$checked = ' checked="checked"';
}
$code_title = get_the_title( $code->description );
$li = '<div class="editor-training-groups-choice">
<input type="hidden" value="0" name="access_ids['.$code->name.']">
<input type="checkbox" id="access_ids['.$code->name.']" name="access_ids['.$code->name.']" value="1" '.$checked.'/><label for="access_ids['.$code->name.']" class="editor-code-label">'.$code->name.': <span class="limit-length">'.$code_title.'</span></label>
</div>';
if (eri_is_TTA($code->name)) {
$tta_names[] = strtolower($code->name);
if ($is_checked) {
$tta_checked[] = $li;
} else {
$tta[] = $li;
}
} elseif (eri_is_ITA($code->name)) {
$ita_names[] = strtolower($code->name);
if ($is_checked) {
$ita_checked[] = $li;
} else {
$ita[] = $li;
}
} elseif (eri_is_UTA($code->name)) {
$uta_names[] = strtolower($code->name);
if ($is_checked) {
$uta_checked[] = $li;
} else {
$uta[] = $li;
}
}
}
// Title Extension
$title_ext = ' Technical Assistance';
$top_margin = '30px';
$target = "_blank";
// Add the title and list items
if (!empty($tta_checked) || !empty($tta)) {
echo '<div class="training-type-title">Targeted'.$title_ext.': <a href="/wp-admin/edit.php?post_type=page&codes='.implode('%2C',$tta_names).'" target="'.$target.'">('.count($tta_names).')</a></div>';
echo implode('', $tta_checked);
echo implode('', $tta);
}
if (!empty($ita_checked) || !empty($ita)) {
$margin = (!empty($tta_checked) || !empty($tta)) ? ' style="margin-top: '.$top_margin.'"' : '';
echo '<div class="training-type-title"'.$margin.'>Intensive'.$title_ext.': <a href="/wp-admin/edit.php?post_type=page&codes='.implode('%2C',$ita_names).'" target="'.$target.'">('.count($ita_names).')</a></div>';
echo implode('', $ita_checked);
echo implode('', $ita);
}
if (!empty($uta_checked) || !empty($uta)) {
$margin = (!empty($tta_checked) || !empty($tta)) || (!empty($ita_checked) || !empty($ita)) ? ' style="margin-top: '.$top_margin.'"' : '';
echo '<div class="training-type-title"'.$margin.'>Universal'.$title_ext.': <a href="/wp-admin/edit.php?post_type=post&codes='.implode('%2C',$uta_names).'" target="'.$target.'">('.count($uta_names).')</a></div>';
echo implode('', $uta_checked);
echo implode('', $uta);
}
?>
</div>  
<span class="description"><?php _e('Select the Webinar Codes this Training Group has access to.'); ?></span>  
<?php echo $cont3; ?>
<?php
}  
// Save the changes made on the "groups" taxonomy, "add new" form
add_action( 'created_groups', 'save_groups_new_custom_fields', 10, 2 );
function save_groups_new_custom_fields( $term_id, $tt_id ) {  
if ( isset($_POST['organization']) ) {  
add_term_meta( $term_id, 'organization', $_POST['organization'], true );
}
if ( isset($_POST['email_domain']) ) {  
add_term_meta( $term_id, 'email_domain', $_POST['email_domain'], true );
}
if ( isset($_POST['access_ids']) ) {
$aid_checked = [];
$access_ids = $_POST['access_ids'];
foreach ($access_ids as $key=>$value) {
if ($value == '1'){
$aid_checked[] = $key;
}
}
add_term_meta( $term_id, 'access_ids', $aid_checked );
}
if ( isset($_POST['facilitators']) ) {
$fac_checked = [];
$facilitators = $_POST['facilitators'];
foreach ($facilitators as $key=>$value) {
if ($value == '1'){
$fac_checked[] = $key;
}
}
add_term_meta( $term_id, 'facilitators', $fac_checked );
}
}
// Save the changes made on the "groups" taxonomy, "edit" form
add_action( 'edited_groups', 'save_groups_existing_custom_fields', 10, 2 ); 
function save_groups_existing_custom_fields( $term_id, $tt_id ) {  
if ( isset($_POST['organization']) ) {
update_term_meta( $term_id, 'organization', $_POST['organization'], true ); // <-- ONLY UPDATING ONCE
}
if ( isset($_POST['email_domain']) ) {
update_term_meta( $term_id, 'email_domain', $_POST['email_domain'], true ); // <-- ONLY UPDATING ONCE
}
if ( isset($_POST['access_ids']) ) {
$aid_checked = [];
$access_ids = $_POST['access_ids'];
foreach ($access_ids as $key=>$value) {
if ($value == '1'){
$aid_checked[] = $key;
}
}
update_term_meta( $term_id, 'access_ids', $aid_checked );
}
if ( isset($_POST['facilitators']) ) {
$fac_checked = [];
$facilitators = $_POST['facilitators'];
foreach ($facilitators as $key=>$value) {
if ($value == '1'){
$fac_checked[] = $key;
}
}
update_term_meta( $term_id, 'facilitators', $fac_checked );
}
}

Interestingly enough my checkbox fields are updating fine every single time, so something is going on with the text fields.

$299 Affordable Web Design WordPress

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

update_term_meta() only updating once on certain meta keys

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.