Last active
July 20, 2020 12:36
-
-
Save sabrina-zeidan/5e86fbc809cdc2ea90b9ecf596919694 to your computer and use it in GitHub Desktop.
Get post/term slug from any type of URL
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 | |
//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