CodeBriefly
Tech Magazine

How to Create Taxonomy Term Meta Data in WordPress

0 3,787

Get real time updates directly on you device, subscribe now.

In this article, we will discuss  “how to create taxonomy term meta data in WordPress”. We will create custom term metadata for our user taxonomy. You have to check the following post for a better understanding.

How to Create Taxonomy for Users in WordPress

Follow the steps to create taxonomy term meta data.

Step 1: Add meta field to add new term form

We are using hook “{taxonomy}_add_form_fields”. Use the following code snippet and add this in the theme functions.php” file.

function cb_add_term_fields( $taxonomy ) {
  echo '<div class="form-field">
    <label for="cb_custom_meta_data">Custom Meta Data Field</label>
    <input type="text" name="cb_custom_meta_data" id="cb_custom_meta_data" />
    <p>Description for meta field goes here.</p>
  </div>';
} 
add_action( 'departments_add_form_fields', 'cb_add_term_fields' );

After updating the code, add term screen looks like this:

How to Create Taxonomy Term Meta Data in WordPress


Step 2: Add meta field to edit term form

We are using hook “{taxonomy}_edit_form_fields”. Use the following code snippet and add this in the theme functions.php” file.

function cb_edit_term_fields( $term, $taxonomy ) {
  $value = get_term_meta( $term->term_id, 'cb_custom_meta_data', true );
  echo '<tr class="form-field">
    <th>
      <label for="cb_custom_meta_data">Custom Meta Data Field</label>
    </th>
    <td>
      <input name="cb_custom_meta_data" id="cb_custom_meta_data" type="text" value="'.esc_attr($value).'" />
      <p class="description">Description for meta field goes here.</p>
    </td>
  </tr>';
}
add_action( 'departments_edit_form_fields', 'cb_edit_term_fields', 10, 2 );

After updating the code, the edit term screen looks like this:

How to Create Taxonomy Term Meta Data in WordPress

Step 3: Save term metadata

In the end, we have to save our term metadata field value to our database. To achieve this, we are using the WordPress existing hooks such as “created_{taxonomy}” and “edited_{taxonomy}”. Here, the same callback function “cb_save_term_fields” is used with those hooks.

function cb_save_term_fields( $term_id ) {
  update_term_meta(
    $term_id,
    'cb_custom_meta_data',
    sanitize_text_field( $_POST[ 'cb_custom_meta_data' ] )
  );
}
add_action( 'created_departments', 'cb_save_term_fields' );
add_action( 'edited_departments', 'cb_save_term_fields' );

That’s it, hope you like this article. I’m trying to explain “How to Create Taxonomy Term Meta Data”. If any query please feel free to add your comment, also you can share your feedback 🙂


You may like:

Display WordPress Custom Taxonomy in Dropdown

Create Taxonomy for Users in WordPress

Child Theme Importance in WordPress

How to Make WordPress Secure

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

Get real time updates directly on you device, subscribe now.

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More