Ok, so I’m feeling generous and will add another answer 🙂
This code will run every time a post is created or updated, and it will add a term
to your teacher
taxonomy with a name that is the same as the post title.
add_action('save_post', 'my_create_teacher_term_from_post');
function my_project_updated_send_email($post_id){
/** Ensure that this is not a revision */
if(wp_is_post_revision($post_id))
return;
/** Get the name of the teacher that has been added/updated */
$post_title = get_the_title(post_id);
/** Check to see if a term with the same name as the teacher exists... */
if(!term_exists($post_title, 'teacher')) : // It does not...
/** Insert the new term */
wp_insert_term($post_title, 'teacher');
endif;
}
Note – If you only want this action to occur when a post is created, not updated, replace the save_post
hook with wp_insert_post
.
Additional reading
-
WordPress action hooks – http://codex.wordpress.org/Plugin_API/Action_Reference
These will be essential to you going forward, and I fully encourage you to root through the core to see which hooks are available – just be careful!
-
The
save_post
action hook – http://codex.wordpress.org/Plugin_API/Action_Reference/save_postUsed in the example above, this hook is called everytime a post is created/updated.
-
The
wp_insert_post
action hook – http://codex.wordpress.org/Plugin_API/Action_Reference/wp_insert_postYou can use this hook instead if you wish, it’s only called when a post is updated though, not created. And should you ever need to use both this hook and the
save_post
hook, note that this one is called aftersave_post
. -
The
term_exists
function – http://codex.wordpress.org/Function_Reference/term_existsThis is how you’ll see if a term with a name that matches the title of the teacher you’ve just added already exists.
-
The
wp_insert_term
function – http://codex.wordpress.org/Function_Reference/wp_insert_termFinally this is how you acutally create a term progamatically.