0

I have a few people working on a website with me, and they need to create custom taxonomy terms using a very simple set of rules; however, they do not seem to understand how to do it. So, I'm trying to create a filter of some sort that will automatically replace their values with corrected ones. For instance, this is how they should look:

[name] -> UTA_1234 // type of training + _ + post id of training
[slug] -> uta_1234 // which should be automatic if left blank
[description] -> 1234 // just the post id

Seems simple, right? Well they keep making them lowercase, using dashes instead of underscores, and forgetting to add the description. :(

So here is what I am trying to do that is not working:

/**
 * 'codes' is the taxonomy slug
 */
add_action( 'created_codes', 'eri_created_incorrect_access_id', 10, 2 );
function eri_created_incorrect_access_id( $term_id, $tt_id ) {
    // Catch the term info
    $name = $_POST['name'];
    $slug = $_POST['slug'];
    $desc = isset($_POST['description']) ?? '';

    // Replace dashes with underscores, and make name uppercase
    $name = strtoupper(str_replace('-', '_', $name));
    $slug = str_replace('-', '_', $slug);

    // Check if there is a description
    if (!$desc || $desc == '') {

        // Then we split the name and add the post id to the description
        $split_name = explode('_', $name);
        $desc = $split_name[1];
    }

    // Now add the items
    add_term_meta( $term_id, 'name', $name, true );
    add_term_meta( $term_id, 'slug', $slug, true );
    add_term_meta( $term_id, 'description', $desc, true );
}

The same concept works for additional custom fields, but not the default fields

1 Answer 1

1

The default fields name, slug and description are not term metadata, so you should instead use wp_update_term() to update those fields.

So just replace those three add_term_meta() with:

wp_update_term( $term_id, 'codes', [
    'name'        => $name,
    'slug'        => $slug,
    'description' => $desc,
] );

Additionally, instead of using $_POST, I would use get_term() to get the details of the term that has just been created. So for example,

$term = get_term( $term_id );

// Catch the term info
$name = $term->name;
$slug = $term->slug;
$desc = $term->description;
3
  • Thank you @SallyCJ. Unfortunately it's not working for me when creating a new term.
    – Michael
    Dec 20, 2021 at 15:13
  • What's the full code you used? Can you append it at the bottom in your question, or post it on Pastebin.com?
    – Sally CJ
    Dec 21, 2021 at 10:55
  • 1
    Nevermind, silly me needed to sleep on it and come back. I realized today that this does actually work perfectly. I was using it on a different site with a different taxonomy slug, so it wasn't recognizing it. I fixed it. Thanks @SallyCJ
    – Michael
    Dec 21, 2021 at 18:35

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.