Skip to content

Instantly share code, notes, and snippets.

@KnightAlex
Last active May 9, 2023 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KnightAlex/5b3a293196a827bed858edacac63abe2 to your computer and use it in GitHub Desktop.
Save KnightAlex/5b3a293196a827bed858edacac63abe2 to your computer and use it in GitHub Desktop.
Convert string to slug and replace special characters
//convert string to a slug
function get_slug_from_string($atts, $content = '')
{
extract(shortcode_atts(array(
'text' => ''
), $atts));
$slug = strtolower($text);
$slug = str_replace(' ', '-', $slug);
// replace special chars
$from = array(
'à', 'á', 'â', 'ã', 'ä', 'å', 'æ',
'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ',
'ß', 'ç', 'Ç',
'è', 'é', 'ê', 'ë',
'È', 'É', 'Ê', 'Ë',
'ì', 'í', 'î', 'ï',
'Ì', 'Í', 'Î', 'Ï',
'ñ', 'Ñ',
'ò', 'ó', 'ô', 'õ', 'ö',
'Ò', 'Ó', 'Ô', 'Õ', 'Ö',
'š', 'Š',
'ù', 'ú', 'û', 'ü',
'Ù', 'Ú', 'Û', 'Ü',
'ý', 'Ý', 'ž', 'Ž'
);
$to = array(
'a', 'a', 'a', 'a', 'a', 'a', 'a',
'A', 'A', 'A', 'A', 'A', 'A', 'A',
'B', 'c', 'C',
'e', 'e', 'e', 'e',
'E', 'E', 'E', 'E',
'i', 'i', 'i', 'i',
'I', 'I', 'I', 'I',
'n', 'N',
'o', 'o', 'o', 'o', 'o',
'O', 'O', 'O', 'O', 'O',
's', 'S',
'u', 'u', 'u', 'u',
'U', 'U', 'U', 'U',
'y', 'Y', 'z', 'Z'
);
$slug = str_replace($from, $to, $slug);
return $slug;
}
add_shortcode('string-to-slug', 'get_slug_from_string');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment