Last active
August 29, 2015 14:21
-
-
Save kurozumi/5c082545270af6f7d947 to your computer and use it in GitHub Desktop.
【ワードプレス】カテゴリを登録すると自動でスラッグの接尾辞にロケールを付与する【未完成】
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('create_term', function($term_id, $tt_id, $taxonomy){ | |
global $wpdb, $locale; | |
$term = get_term( $term_id, $taxonomy, ARRAY_A ); | |
$locale = strtolower($locale); | |
$slug = sprintf("%s-%s", $term['slug'], $locale); | |
if(preg_match("/-{$locale}$/i", $term['slug'])) | |
return; | |
$duplicate_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id, tt.term_taxonomy_id FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON ( tt.term_id = t.term_id ) WHERE t.slug = %s AND tt.parent = %d AND tt.taxonomy = %s AND t.term_id < %d AND tt.term_taxonomy_id != %d", $slug, $term['parent'], $taxonomy, $term_id, $tt_id ) ); | |
if ( $duplicate_term ) { | |
$wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) ); | |
$wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) ); | |
} else { | |
$wpdb->update($wpdb->terms, compact('slug'), compact( 'term_id' )); | |
} | |
}, 99, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
重複スラッグが登録できない。。