• Is i possible to have in all tags slug their term_id ?

    for example, i have tags usa,argentina,brazil. I wanna have
    site.com/tag/1 site.com/tag/2 site.com/tag/3.

    i know that i can change slug in settings but i need this for all my future tags.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi kramer1711,

    You could write a function that creates the url structure for you. Something like:

    $url = get_site_url();
    $tagBase = ( get_option( 'tag_base' ) ) ? get_option( 'tag_base' ) : 'tag';
    foreach ( get_the_tags() as $tag ) {
        $tagUrl = $url . "/" . $tagBase . "/" . $tag->term_id;
    }

    Then use that function to display your tags.

    Thread Starter kramer1711

    (@kramer1711)

    Hi dbough,
    Thanks for reply. can you write more about it? I know just quite a bit about wp structure and have no knowledge how to use this code properly…

    I assume your setup will recognize URLs formatted this way already?

    Add this to your themes functions.php file. It could use some refactoring but it will get you started.

    This will update the tag url anywhere that tags are displayed with ‘the_tags’:

    function filter_tag_slug_to_id( $content )
    {
    	$dom = new DOMDocument;
    	$dom->loadHTML( $content );
    	foreach ( $dom->getElementsByTagName('a') as $node) {
    	 	if( $node->hasAttribute( 'href' ) ) {
    	 		$url = $node->getAttribute( 'href' );
    	 		$tokens = explode( '/', $url );
    			$term = $tokens[sizeof($tokens)-2];
    			$tagId = get_term_by( 'name', $term, 'post_tag' )->term_id;
    			array_pop( $tokens );
    			array_pop( $tokens );
    			$newUrl = implode( '/', $tokens ) . "/" . $tagId;
    			$content = str_replace( $url, $newUrl, $content );
    	 	}
    	}
    	return $content;
    }
    add_filter ( 'the_tags', 'filter_tag_slug_to_id' );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Tags slug’ is closed to new replies.