0

After two days of research and testing, I am totally stumped on how to correctly parse two unique values from a select field and save each to my database as separate meta key/value pairs. The dropdown in question resides in my WordPress category editor, and the two values I'm trying to save have been registered as term meta fields (with keys of $loc_name and $loc_code).

As for the data itself, I'm populating the dropdown with a csv file that has a column of location names and another column of corresponding location codes, both of which need to be passed along to the database for later use in API calls.

So far, I've been able to save $code_val, but can't figure out how to save $name_val as well?

Here's the complete code for reference...

// REGISTER TERM META

add_action( 'init', 'codename_term_meta' );

function codename_term_meta() {
    $args1 = array(
        'type'      => 'string',
        'description'    => 'A meta key for location name value.',
        'single'        => true,
        'sanitize_callback' => 'sanitize_meta', 
        'default'    => true,
        'show_in_rest'    => true, 
    );
    register_meta( 'term', 'loc_name', $args1 );
    
    $args2 = array(
        'type'      => 'string',
        'description'    => 'A meta key for location code value.',
        'single'        => true,
        'sanitize_callback' => 'sanitize_meta', 
        'default'    => true,
        'show_in_rest'    => true,
    );
    register_meta( 'term', 'loc_code', $args2 );
}

// SANITIZE DATA

function sanitize_meta ( $value ) {
    return sanitize_text_field ($value);
}

// GETTER (will be sanitized)

function ___get_term_meta_text( $term_id, $term_key ) {
  $value = get_term_meta( $term_id, $term_key, true );
  $value = sanitize_meta( $value );
  return $value;
}

// ADD FIELD TO CATEGORY EDIT PAGE

add_action( 'category_edit_form_fields', 'edit_codename_meta' );

function edit_codename_meta( $term ) {

    $name_val  = ___get_term_meta_text( $term->term_id, 'loc_name', true );
    $code_val  = ___get_term_meta_text( $term->term_id, 'loc_code', true );

    if ( ! $name_val )
        $name_val = ""; 
    
    if ( ! $code_val )
        $code_val = ""; 
    
    ?>
    
    <tr class="form-field">
        <th scope="row" valign="top">
            <label for="location_codenames"><?php _e( 'Select Name', '' ); ?></label>
        </th>
        <td>
            <select id="code_names" aria-label="code_names" name="code_names" class="code-names">
            <?php
                $fileHandle = fopen("http://website.com/file/codenames.csv", "r");
                while (($row = fgetcsv($fileHandle, 0, ",")) !== FALSE) {
                        $name_val = $row[0];
                        $code_val = $row[1];
            ?>
                <option value="<?php echo $code_val;?>"><?php echo $name_val;?></option>
            <?php
                }
            ?>
            </select>
        </td>
    </tr> 
<?php }


// SAVE TERM META (on term edit)

add_action( 'edit_category',   'save_codename_meta' );

function save_codename_meta( $term_id ) {

    $old_value_name  = ___get_term_meta_text( $term_id, 'loc_name' );
    $new_value_name = isset( $_POST['code_names'] ) ? sanitize_meta ( $_POST['code_names'] ) : '';
    
    $old_value_code  = ___get_term_meta_text( $term_id, 'loc_code' );
    $new_value_code = isset( $_POST['code_names'] ) ? sanitize_meta  ( $_POST['code_names'] ) : '';


    if ( $old_value_name && '' === $new_value_name ) {
        delete_term_meta( $term_id, 'loc_name' );
    } else if ( $old_value_name !== $new_value_name ) {
        update_term_meta( $term_id, 'loc_name', $new_value_name );
    }
        
    if ( $old_value_code && '' === $new_value_code ) {
        delete_term_meta( $term_id, 'loc_code' );
    } else if ( $old_value_code !== $new_value_code ) {
        update_term_meta( $term_id, 'loc_code', $new_value_code );
    }
}

I'd be really grateful for any assistance in getting this to work!!

BTW, here are the original sources that I'm trying to adapt for this implementation...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.