Last active
August 29, 2015 14:17
-
-
Save janizde/ea6bb5efe791c88021ce to your computer and use it in GitHub Desktop.
Filter fixing WPML appending language codes on term slugs
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
<?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