0

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 training\s 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.

1 Answer 1

0

I figured it out:

update_term_meta( $term_id, 'organization', $_POST['organization'], true );

should have been

update_term_meta( $term_id, 'organization', $_POST['organization'] );

Where the fourth parameter makes it unique.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.