Skip to content

Instantly share code, notes, and snippets.

@janizde
Last active August 29, 2015 14:17
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janizde/ea6bb5efe791c88021ce to your computer and use it in GitHub Desktop.
Save janizde/ea6bb5efe791c88021ce to your computer and use it in GitHub Desktop.
Filter fixing WPML appending language codes on term slugs
<?php
/**
* Filter fixing term slug for WPML plugin
* always (!) removes -{language code} (including repetitions like "-de-de-de") from the end of the slug before it is saved in the database and after WPML modified the slug.
* Note that this will always happen, even if you typed in the language code yourself in the input.
* The function will not strip combinations of different language codes like "-de-en-de" (this would certainly result in "-de-en").
*
* @author Jannik Portz (@janizde)
* @param string $slug The slug coming from WPML_Term_Translations::pre_term_slug_filter
* @return string The slug without language codes at the end
*/
function wpml_fix_filter_term_slug ( $slug ) {
/** You may have to change this, depending on what languages you are using */
$language_codes = array( 'de', 'en', 'ru', 'sp', 'it' );
foreach ( $language_codes as $l ) :
$pattern = '/^([a-z0-9-]*?)(-'. $l .')+$/';
$count = 0;
$slug = preg_replace( $pattern, '$1', $slug, -1, $count );
if ( $count != 0 )
break;
endforeach;
return $slug;
}
add_filter( 'pre_term_slug', 'wpml_fix_filter_term_slug', 11, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment