Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i have tried %tag% in permalinks but it dont showing tag name in the permalinks... What i wanna do is to use the first tag name in the permalinks. So, how to do it? Thanks

share|improve this question
    
maybe try @JanFabry's answer here wordpress.stackexchange.com/questions/7004/… –  Mridul Aggarwal Nov 11 '12 at 13:55

1 Answer 1

up vote 1 down vote accepted

Use add_rewrite_tag() to register the placeholder, and filter post_link to insert the correct tag. Use get_the_tags() to get the tags for a post.

Sample plugin I have used in a project:

<?php
/**
 * Plugin Name: T5 Tag as rewrite tag
 * Description: Use <code>%tag%</code> in permalinks.
 * Version:     2012.09.02
 * Author:      Thomas Scholz
 * Author URI:  http://toscho.de
 * Licence:     MIT
 * License URI: http://opensource.org/licenses/MIT
 */

add_action( 'init', array ( 'T5_Rewrite_Tag_Tag', 'init' ) );

/**
 * Adds '%tag%' as rewrite tag (placeholder) for permalinks.
 */
class T5_Rewrite_Tag_Tag
{
    /**
     * Add tag and register 'post_link' filter.
     *
     * @wp-hook init
     * @return  void
     */
    public static function init()
    {
        add_rewrite_tag( '%tag%', '([^/]+)' );
        add_filter( 'post_link', array( __CLASS__, 'filter_post_link' ) , 10, 2 );
    }

    /**
     * Parse post link and replace the placeholder.
     *
     * @wp-hook post_link
     * @param   string $link
     * @param   object $post
     * @return  string
     */
    public static function filter_post_link( $link, $post )
    {
        static $cache = array (); // Don't repeat yourself.

        if ( isset ( $cache[ $post->ID ] ) )
            return $cache[ $post->ID ];

        if ( FALSE === strpos( $link, '%tag%' ) )
        {
            $cache[ $post->ID ] = $link;
            return $link;
        }

        $tags = get_the_tags( $post->ID );

        if ( ! $tags )
        {
            $cache[ $post->ID ] = str_replace( '%tag%', 'tag', $link );
            return $cache[ $post->ID ];
        }

        $first              = current( (array) $tags );
        $cache[ $post->ID ] = str_replace( '%tag%', $first->slug, $link );

        return $cache[ $post->ID ];
    }
}
share|improve this answer
    
hey, thanks for your reply... understanding my level.. i'm just a beginner in wp ... so will you tell me where to edit this codes, or any articles... –  Hasan Nov 11 '12 at 17:11
    
Install the code as plugin, then go to wp-admin/options-permalink.php and use %tag% as placeholder like %postname% or %year. –  toscho Nov 12 '12 at 0:51
    
hello, i am facing a new kind of problem while a post has multiple tags, the first tag i have put to use in permalinks is not working either other tags are being used in tags.... any idea what to do? Thanks –  Hasan Nov 15 '12 at 10:15
    
@Hasan I don’t really understand what you are trying to say. Please try my latest plugin T5 Rewrite and use %tag% as placeholder in your permalink settings. That should work. –  toscho Nov 15 '12 at 10:33
    
ya,, your plugin is great .. how to use the %_custome_% permalinks settings in email posting? I am posting in my wp blog through email using postie plugin.... –  Hasan Nov 15 '12 at 13:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.