Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active August 29, 2015 14:21
Show Gist options
  • Save kurozumi/5c082545270af6f7d947 to your computer and use it in GitHub Desktop.
Save kurozumi/5c082545270af6f7d947 to your computer and use it in GitHub Desktop.
【ワードプレス】カテゴリを登録すると自動でスラッグの接尾辞にロケールを付与する【未完成】
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);
@kurozumi
Copy link
Author

重複スラッグが登録できない。。

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