adamdoe
6/29/2018 - 6:30 PM

CRUD custom taxonomy meta wordpress

CRUD custom taxonomy meta wordpress

<?php 

add_action( '{taxonomy}_add_form_fields', 'add_feature_group_field', 10, 2 );
function add_feature_group_field($taxonomy) {
    global $meta_name;
    ?>

    <div class="form-field">
      <label for="meta_name">Meta Name</label>
      <input name="meta_name" id="meta_name" type="text" value="<?php echo $meta_name; ?>" size="40" aria-required="true">
    </div>

    <?php
}

add_action( 'created_{taxonomy}', 'save_feature_meta', 10, 2 );
add_action( 'edited_{taxonomy}', 'save_feature_meta', 10, 2 );

function save_feature_meta( $term_id, $tt_id ){
    if( isset( $_POST['meta_name'] ) && !empty($_POST['meta_name']) ){
        add_term_meta( $term_id, 'meta_key', $_POST['meta_name'], true );
    }
}

add_action( '{taxonomy}_edit_form_fields', 'edit_feature_group_field', 10, 2 );

function edit_feature_group_field( $term, $taxonomy ){
                
    global $meta_name;
          
    // get current group
    $meta_name = get_term_meta( $term->term_id, 'meta_key', true );
                
    ?>

    <tr class="form-field">
      <th scope="row"><label for="meta_name">Meta Name</label></th>
      <td><input name="meta_name" id="meta_name" type="text" value="<?php echo $meta_name ?>" size="40" aria-required="true">
      </td>
    </tr>

    <?php
}


 ?>