Site icon Hip-Hop Website Design and Development

How to filter custom taxonomy term name, slug, and description?

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