Skip to content

Instantly share code, notes, and snippets.

@sabrina-zeidan
Last active July 20, 2020 12:36
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 sabrina-zeidan/5e86fbc809cdc2ea90b9ecf596919694 to your computer and use it in GitHub Desktop.
Save sabrina-zeidan/5e86fbc809cdc2ea90b9ecf596919694 to your computer and use it in GitHub Desktop.
Get post/term slug from any type of URL
<?php
//Examples of input
$input = "http://www.example.com/cat/cat-child/slug/";
$input = "http://www.example.com/cat/slug";
$input = "http://www.example.com/cat/slug/";
$input = "http://www.example.com/cat/slug/?dsss&efsdc=dc";
//This line will always return "slug"
$slug = basename(strtok(str_replace(' ', '', htmlspecialchars($input )), '?')).PHP_EOL;
echo $slug;
//Useful for further use with get_term_by('slug', $slug, $tax);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment