• devmania

    (@devmania)


    Hi,

    I’m looking to customize the slugs of terms (tags/categories) just before they are stored in the database. While I’ve found the wp_unique_term_slug hook, it seems to work as intended only during term creation, not when editing existing terms.

    Is there a hook or an alternative method that would allow for consistent filtering of term slugs in both the creation and editing processes?

    (Note: I’m accustomed to the behavior of wp_unique_post_slug, which handles both cases for post slugs, and I’m looking for a similar capability with wp_unique_term_slug).

    Thanks.

Viewing 1 replies (of 1 total)
  • niyaswp

    (@niyaswp)

    Hello,

    You can try the following code, maybe it will help you:-

    function insert_taxonomy_content( $term_id, $tt_id, $taxonomy ){
        // only insert content on certain taxonomies
        if ( $taxonomy === 'some_custom_taxonomy' ){
    
             // unhook this function so it doesn't loop infinitely
             remove_action('edited_term', 'insert_taxonomy_content');
    
             $content = "Some Content";
             $update_args = array(
                 'description' => $content,
             );
    
             // update the post, which calls save_post again
             wp_update_term( $term_id, $taxonomy, $update_args );
    
             // re-hook this function
             add_action('edited_term', 'insert_taxonomy_content');
         }
     }
    add_action('edited_term', 'insert_taxonomy_content', 10, 3);

    You need to change the slug, name and description as required!

Viewing 1 replies (of 1 total)
  • The topic ‘wp_unique_term_slug fired only when adding a term’ is closed to new replies.