Skip to content

Instantly share code, notes, and snippets.

@mohsinr
Last active January 27, 2017 12:49
Show Gist options
  • Save mohsinr/0322850115acec978c109afc52c6f388 to your computer and use it in GitHub Desktop.
Save mohsinr/0322850115acec978c109afc52c6f388 to your computer and use it in GitHub Desktop.
WordPress Taxonomies , Remove Trailing And Leading Spaces from Terms' Names
<?php
/**
Need to run this code just ONCE,
so remove it after one pageload once spaces are removed
**/
# List of Taxonomies with terms which have leading spaces
$taxos = array('rooms', 'settings', 'views', 'features');
foreach ($taxos as $taxo) {
$terms = get_terms( array('taxonomy' => $taxo, 'hide_empty' => false) );
foreach ($terms as $term) {
# Need to convert hidden spaces into html entities for easy find/replace
$name = htmlentities($term->name, null, 'utf-8');
$name_new = preg_replace("/&nbsp;/",'',$name);
wp_update_term( $term->term_id, $taxo, array(
'name' => $name_new
) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment