0

I've created a Custom Taxonomy and I want to hook a function when User adds new Term to my Custom Taxonomy. Can someone please show me how to use following actions in Wordpress?

  1. create_term
  2. edit_term
  3. delete term

All these actions are defined in wp-includes/taxonomy.php as follows:

do_action("create_term", $term_id, $tt_id, $taxonomy);
do_action("edit_term", $term_id, $tt_id, $taxonomy);
do_action('delete_term', $term, $tt_id, $taxonomy);

Whenever I try to hook my function with add_action, the Custom Taxonomy page works indifferently. Page does not refresh when I add/edit new Taxonomy Term and attempting to delete any existing term gives me error: "An undefined error occurred." Actually all changes are getting registered to Database... page doesn't refresh on add/edit, but the Term is added/changed successfully. Even delete action actually deletes the term; I can see the changes when I refresh the page.

I think there is something which the function may need to return... something is holding the Wordpress back and I just don't know what's going wrong.

Waiting for your valuable reply...

Regards,

Ruturaaj.

2 Answers 2

4

There's not much to it, you don't need to return anything. Note that you also can't output anything to the browser, that will cause it to fail.

function my_create( $term_id, $tt_id, $taxonomy ){
    // do some stuff
}
add_action( 'create_term', 'my_create', 10, 3 );

You mention using this with a custom taxonomy, note that there are also actions that let you hook specific taxonomies:

do_action("create_$taxonomy", $term_id, $tt_id);
5
  • Oh yes... I just checked my code and there was a print_r in it; had nothing to do with the code logic, I just added that to the code to see if the array is getting filled correctly! :-)
    – Ruturaj
    Commented May 2, 2012 at 18:55
  • Actually, I was trying to load the Terms of my Custom Taxonomy in a drop-down. I was facing problems in getting terms with get_terms as well as get_categories. I posted that query here but no luck. So, I had a trick in mind: I fill an array every time new term is added, edited, deleted. I use add_option to set this array as option-value and use it on Options Page to fill-in drop-down combo box. The problem is, I've to refresh the page each time new term is added, edited, deleted. Is there any work-around for this? Ideal will be if you can show me proper way to read Terms in functions.php.
    – Ruturaj
    Commented May 2, 2012 at 19:00
  • @RuturaajPatki where are you using this dropdown? which page do you need to refresh? the taxonomy admin pages use ajax calls to add and delete terms, you'd have to update any additional elements on those pages via ajax as well.
    – Milo
    Commented May 2, 2012 at 20:34
  • I'm using the drop-down on Theme Options page & this is the page that I need to refresh. The code is in options.php file which is included to the functions.php after taxonomy code. The array is filled correctly. Just visiting the options page by clicking the "Site Options" menu under "Appearances" doesn't serve the purpose. The function that creates Theme Options page is "mytheme_admin" and as you can expect, it's called like this: add_theme_page("Theme Options", "Site Options", 'edit_theme_options', basename(FILE), 'mytheme_admin');
    – Ruturaj
    Commented May 4, 2012 at 9:49
  • @Milo, you mentioned that "Note that you also can't output anything to the browser, that will cause it to fail." I was also trying to echo something to be able to check the input/output. Usually, you can do that in action hooks. I was wondering if you could explain further why you can't echo anything on the screen here. Many thanks.
    – dashaluna
    Commented Feb 4, 2014 at 14:29
1

To update this, since wp 3.5, the "delete_term" action provides 4 parameters now (http://core.trac.wordpress.org/ticket/20645).

do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );

where $term is the term id and $deleted_term is the term object.

1
  • This looks more like a comment an existing answer than it does an answer in itself, but there is no need to use all 4 parameters if you don't need, and don't ask for, all four.
    – s_ha_dum
    Commented Jul 2, 2013 at 20:03

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.