1

I'd like to give administrators and editors the option to specify colors for categories they create. They'd enter in the hex colors they prefer e.g. #000.

I'm experiencing an odd issue in that the first time I create a category and pop in a color, it doesn't save the term meta but does on every subsequent category I create. For example, if the first category is titled 'Category 1' with the hex color #1A282A, no term meta is saved.

If I then create a second category named 'Category 2' with a hex color, it saves it and it continues to do so with any future category. I can't quite figure out why the term meta isn't added or updated when it's the first category.

What am I missing?

function mt_set_color_category( $term ) {
    $color = get_term_meta( $term->term_id ?? false, '_term_color', true );
    if( $color == '' ) $color = '#ffffff';
  ?>
    <tr class="form-field">
        <th scope="row"><label for="term-color">Category Color</label></th>
        <td>
            <input name="_term_color" value="<?php echo esc_attr( $color ); ?>" class="color" id="term-color" />
        </td>
    </tr>
  <?php
}
add_action( 'category_add_form_fields', 'mt_set_color_category' );
add_action( 'category_edit_form_fields', 'mt_set_color_category' );

function mt_set_color_category_term_meta( $term_id ) {
    if( isset( $_POST['_term_color'] ) && ! empty( $_POST['_term_color'] ) ) {
        update_term_meta( $term_id, '_term_color', sanitize_hex_color( $_POST['_term_color'] ) );
    } else {
        delete_term_meta( $term_id, '_term_color' );
    }
}
add_action( 'created_category', 'mt_set_color_category_term_meta' );
add_action( 'edited_category',  'mt_set_color_category_term_meta' );
9
  • @SallyCJ - I came across your answer - wordpress.stackexchange.com/questions/407671/where-to-use-nonce as an example. Am I overlooking anything?
    – Motivated
    Jul 21, 2023 at 7:12
  • The hooks should be create_category and edit_category You've added ed to the naming which are different hooks that fire after the term cache has been cleaned :-) (also note that these are generic template hooks and you can use them for other taxonomies than just "category")
    – jave.web
    Jul 24, 2023 at 8:11
  • @jave.web, thanks. I did try it with create_category and edit_category and it results in the same behavior. Also, what difference does it make after the term cache has been cleared?
    – Motivated
    Jul 25, 2023 at 7:39
  • Oh sorry I misread the question my comment was off topic - does the saving on "Category 1" NEVER works? Or does it start to work after creating "Category 2"? Have you tried changing the title, and mainly, the slug of the first category? (btw: the cache thing could be different if you would want to display data in the same call that saves them (I'm guessing))
    – jave.web
    Jul 28, 2023 at 19:00
  • @jave.web, it never works when saving the first category with the new field filled in. It does work after the first category has been added i.e. when I add the second category with a value in the new field. Yes, I have tried different titles and slugs. The same behavior occurs with a fresh installation of WordPress with no plugins (other than the default).
    – Motivated
    Jul 28, 2023 at 20:03

1 Answer 1

0

The issue you are experiencing might be due to the action hook you're using to save the term meta. The created_category hook fires after the term is created, but it doesn't ensure that all fields are available at that point.

To ensure that the term meta is being saved correctly even for the first category, you might want to use the create_term or create_category hooks. These hooks are fired after a new term or category is created in the database and all information is already available.

Here is how you might modify your code:

function mt_set_color_category_term_meta( $term_id ) {
    if( isset( $_POST['_term_color'] ) && ! empty( $_POST['_term_color'] ) ) {
        update_term_meta( $term_id, '_term_color', sanitize_hex_color( $_POST['_term_color'] ) );
    } else {
        delete_term_meta( $term_id, '_term_color' );
    }
}
add_action( 'create_term', 'mt_set_color_category_term_meta' );
add_action( 'edit_term',  'mt_set_color_category_term_meta' );

You may also want to consider using the edit_term action instead of edited_category for consistency and to ensure it works for all term types, not just categories. The edit_term action fires after a term has been updated, and it works for any taxonomy.

1
  • thanks. I tried that too and it results in the same behavior.
    – Motivated
    Jul 25, 2023 at 7:43

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.