I have created a custom post type plugin that when the user assigns the post to a taxonomy (termed 'classification' in the site), it is added to the url slug.
So basically it renders: site.com/data/{classification}/{page-title}
However, I'm not sure how to remove the taxonomy (classification) from the slug when the user does not select one. My code is below:
function classification_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%classification%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'classification');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'no-classification';
return str_replace('%classification%', $taxonomy_slug, $permalink);
Obviously the else $taxonomy_slug = 'no-classification';
is what the slug defaults to when a taxonomy is not chosen.
But how would I edit that line so the slug, when no taxonomy is assigned, would revert to the root so it would read: site.com/data/{page-title} ?
The full plugin is on my github: https://github.com/tgelles/dr12-post-types