Skip to content

Instantly share code, notes, and snippets.

@nciske
Created July 30, 2013 21:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nciske/6117419 to your computer and use it in GitHub Desktop.
Save nciske/6117419 to your computer and use it in GitHub Desktop.
Email site admin when a term slug changes (any taxonomy). Cleaner single taxonomy version here: [https://gist.github.com/nciske/6117497]
<?php
add_action( 'edit_terms', 'example_terms_edit' );
function example_terms_edit( $term_id ){
global $wpdb;
global $example_old_slug;
$example_old_slug = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
}
add_action( 'edited_terms', 'example_terms_edited' );
function example_terms_edited( $term_id ){
global $wpdb;
global $example_old_slug;
$new_slug = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
if( $new_slug != $example_old_slug )
wp_mail( get_option('admin_email') , 'Slug changed for term #'.$term_id, "Old Slug: {$example_old_slug}\r\nNew Slug: {$new_slug}" );
}
@JDGrimes
Copy link

Why not use a single function and a static variable? That will save having to use a global. You will just need to check the value of the static variable to see which hook is being called, and you will need to clear it after you've checked whether the slug has been updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment