Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.