1

I am trying to insert content into a term's description field on save.

// insert stuff into description field of taxonomy
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('edit_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('edit_term', 'insert_taxonomy_content');
     }
 }
add_action('edit_term', 'insert_taxonomy_content', 10, 3);

The adding of the content works, but now I can't change the title of an existing term anymore. I also can't add a new term.

I think this is pointing me in the right direction: https://wordpress.stackexchange.com/a/183852/10595

The array passed in gets merged with the data already in the DB.

So how could I capture a new title and/or slug entered into the Name / Slug field to pass it on to wp_update_term?

I also tried this as per cjbj's suggestion:

// insert stuff into description field of taxonomy
function insert_taxonomy_content( $term_id, $taxonomy ){
    // only insert content on certain taxonomies
    if ( $taxonomy === 'some_custom_taxonomy' ){

         // unhook this function so it doesn't loop infinitely
         remove_action('edit_terms', '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('edit_terms', 'insert_taxonomy_content');
     }
 }
add_action('edit_terms', 'insert_taxonomy_content', 10, 2);

This lets me edit titles and slugs again, but now the description does not get updated.

  • Pitty that didn't work. But it does show that doing two updates in a row leads to the first one getting lost. – cjbj Sep 17 '16 at 10:10
2

So, thanks to cjbj I finally found the correct answer! I needed to use edited_term instead of edit_term. Very subtle difference. edited_term fires after a term has been saved.

// insert stuff into description field of taxonomy
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);
1

The edit_term hook fires "after a term has been updated, but before the term cache has been cleaned". So every time you update a term you fire a function that updates the term again, but only its description. I haven't tested this, but I could imagine that somewhere in this process WP looses the first update and thinks the second update is the only one that matters.

Anyway, I suggest you try the edit_terms hook, which fires before the term is being updated. In that way, your update doesn't interrupt the regular update process.

UPDATE

This may become a bit of a guessing game, but there are a couple more hooks in wp_update_term that you may try: edited_terms, edit_term_taxonomy and edited_term_taxonomy.

The point remains, however, that you are calling wp_update_term halfway inside wp_update_term. That function manipulates the global variable $wpdb. So the database is first changed by the first part of the outer call, then by the inner call (your function) and then again by the second part of the outer call.

Perhaps a better course of action would be to ditch the call to wp_update_terms in your function. Instead you could try to update the description immediately using $wpdb->update, so no other manipulation of the database takes place inside your hook function. I couldn't test this, but tracing how wp_update_terms handles the description it would be something like this:

$description = "Some Content";
$wpdb->update( $wpdb->term_taxonomy, compact( 'description' ));
  • See my update, still no luck. – Florian Sep 16 '16 at 18:53
  • Thanks for helping me in the right direction. So many hooks with similar names! – Florian Sep 17 '16 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.