Forum Replies Created

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter amidknight

    (@amidknight)

    @tommyk – that seems like avoidance. Especially since the problem is solvable, and works the same way the %category% delimiter does. I wonder if they will be removing the %category% delimiter as well?

    The final site has gone live – and the tag code listed above works. Unfortunately, because of known security issues I do not feel comfortable placing the URL here at this time to show it in the actual context. Further, the client is pleased and understands that categories are for the “essence” of the post, while tags are for the “type.”

    Cheers

    Thread Starter amidknight

    (@amidknight)

    The Fix

    (Will mark as resolved once included with initial download of WordPress.)

    The code below will make it work the way the Codex says it should.

    http://joshbruce.com/tag_url/

    Until this is included with WordPress itself – if you use %tag% in your permalink structure – get_permalink(), the_permalink(), and automatic comment redirect functionality will not work properly.

    Also, there should be an inclusion of a default “Tag Base” portion added to the code (similar to the $category section); however, I don’t need it right now.

    File: /includes/link-template.php
    Function: get_permalink();

    Changes are commented via standard PHP ‘//’ comment method.

    /**
     * Retrieve full permalink for current post or post ID.
     *
     * @since 1.0.0
     *
     * @param int $id Optional. Post ID.
     * @param bool $leavename Optional, defaults to false. Whether to keep post name or page name.
     * @return string
     */
    function get_permalink($id = 0, $leavename = false) {
    	$rewritecode = array(
    		'%year%',
    		'%monthnum%',
    		'%day%',
    		'%hour%',
    		'%minute%',
    		'%second%',
    		$leavename? '' : '%postname%',
    		'%post_id%',
    		'%category%',
    		'%tag%', // removes %tag% delimiter
    		'%author%',
    		$leavename? '' : '%pagename%',
    	);
    
    	if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter )
    		$post = $id;
    	else
    		$post = &get_post($id);
    
    	if ( empty($post->ID) ) return false;
    
    	if ( $post->post_type == 'page' )
    		return get_page_link($post->ID, $leavename);
    	elseif ($post->post_type == 'attachment')
    		return get_attachment_link($post->ID);
    
    	$permalink = get_option('permalink_structure');
    
    	if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending')) ) {
    		$unixtime = strtotime($post->post_date);
    
    // begin permalink inclusion for %tag%
    		$tag = '';
    		if ( strpos($permalink, '%tag%') !== false ){ // found %tag% in permalink structure
    			$tags = get_the_tags($post->ID); // get associated tags (array)
    			if ( $tags ) {
    				usort($tags, '_usort_terms_by_ID'); // order by ID
    				$tag = $tags[0]->slug; // use the lowest number tag
    			}
    		}
    // end coding for %tag% replacemnt
    
    		$category = '';
    		if ( strpos($permalink, '%category%') !== false ) {
    			$cats = get_the_category($post->ID);
    			if ( $cats ) {
    				usort($cats, '_usort_terms_by_ID'); // order by ID
    				$category = $cats[0]->slug;
    				if ( $parent = $cats[0]->parent )
    					$category = get_category_parents($parent, false, '/', true) . $category;
    			}
    			// show default category in permalinks, without
    			// having to assign it explicitly
    			if ( empty($category) ) {
    				$default_category = get_category( get_option( 'default_category' ) );
    				$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
    			}
    		}
    
    		$author = '';
    		if ( strpos($permalink, '%author%') !== false ) {
    			$authordata = get_userdata($post->post_author);
    			$author = $authordata->user_nicename;
    		}
    
    		$date = explode(" ",date('Y m d H i s', $unixtime));
    		$rewritereplace =
    		array(
    			$date[0],
    			$date[1],
    			$date[2],
    			$date[3],
    			$date[4],
    			$date[5],
    			$post->post_name,
    			$post->ID,
    			$category,
    			$tag, // for implosion of permalink
    			$author,
    			$post->post_name,
    		);
    		$permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
    		$permalink = user_trailingslashit($permalink, 'single');
    		return apply_filters('post_link', $permalink, $post, $leavename);
    	} else { // if they're not using the fancy permalink option
    		$permalink = get_option('home') . '/?p=' . $post->ID;
    		return apply_filters('post_link', $permalink, $post, $leavename);
    	}
    }
    Thread Starter amidknight

    (@amidknight)

    Understood. However, “not recommended” does not equal “impossible”. Right now it’s not possible to put %tag% in a custom permalink structure, and have get_permalink() and the_permalink() functions return the properly formatted URL – no matter where it appears in the structure. And, for logistics – I want to – the loss of performance is moot.

    Finally, the get_permalink() and the_permalink() functions should still return the URL without the %tag% delimiter – as it does with every other structure delimiter listed on the codex page referenced multiple times in the thread.

    Again, the bottom line is, if %tag% is used anywhere (beginning, middle, or end) in a custom permalink structure – get_permalink() and the_permalink() do not work.

    Thread Starter amidknight

    (@amidknight)

    Addendum: Regarding Themes

    The links above are the default WordPress theme – no changes. If anyone would like to experiment (see for themselves):

    1. Install WordPress 2.7+
    2. Login & set the permalink structure to: /%tag%/%year%/%monthnum%/%postname% (or anything with %tag% in it)
    3. Create some tags
    4. Create some posts referencing the tags
    5. Then preview the site
    6. follow a title link

    No other changes are necessary to demonstrate the error.

    Thread Starter amidknight

    (@amidknight)

    No worries on the getting confused. It’s difficult to convey some things via text. 🙂

    I wanted to respond to your points – but, didn’t want to make the situation more confusing. So, the part in the blockquote is the important bit – and I could/should start a new thread using just that.

    http://joshbruce.com/tag_url/

    Changed Permalink structure as mentioned. And, unfortunately it doesn’t work the way it should/would (considering the other bits work). the_permalink(), as suspected, uses the root category “photography” – not the tags “blog/news” – which it would if “%tag%” were being sanitized properly. As I said before I’m trying to avoid having a complex category structure – the way I would have to set up the categories to get this permalink structure to create the URLs desired would be:

    • news
    • photography
    • weddings
    • flowers
    • blog
    • photography
    • wedddings
    • flowers

    That’s bad UX for the admins. Using this method the author will have to check the news/blog category, then check whatever is appropriate under that tree – not idiot proof. What I want are two separate lists (which, with the introduction of tags – I should be able to do):

    Tags:

    • news
    • blog

    Categories:

    • photography
    • weddings
    • flowers

    Which drastically reduces the complexity for the Admin/Author. They choose the type of post (news/blog) – then the content type (photography/wedding/flowers).

    I’m stuck on using tags partially because it makes for a much more elegant Authoring experience in this case. And, because WordPress says it can be used. But, WordPress is not sanitizing the “%tag%” in some cases.

    have_comments() – was introduced in 2.7 to begin the new comment display loop. It is a binary (true/false) function – which determines if the post has comments associated with it (this is now working properly).

    comments_number() – is a template tag – which is used in the default theme’s comments.php file (version 2.7+) – also works properly.

    wp_list_comments() – I don’t want it to return category stuff. I want it too read the URL – get the appropriate comments – and display them. And, as of now, it does that properly – but, it didn’t when I initially posted.

    The form doesn’t redirect to the proper post – because WordPress is creating a URL for the post based on incorrect sanitation of the permalink structure – hence %tag% not being removed. Which can be hacked by adding the following to the comment form:

    <input type="hidden" name="redirect_to" value="<?php print($_SERVER['SCRIPT_URI']); ?>" />

    However, if WordPress were sanitizing the %tag% properly – based on the personalized permalink structure – it wouldn’t be needed.

    Thinking of the recipe book analogy. Categories are hierarchical – macro to micro. Tags are independent of hierarchy. Therefore, let’s use an apple pie as an example. This method would Tag the pie as “dessert”. Category = pie. Pie could then have subcategories for type: Pie->apple. Another root category: Ingredients. With subcategories: Ingredients->apples, Ingredients->flour, Ingredients->eggs, etc. Therefore, when a user put in the URL:

    domain.com/dessert – this recipe would appear.

    Or:

    domain.com/category/pie – this recipe would, again, appear.

    Or:

    domain.com/category/ingredients/apples

    Regarding the tag cloud – I don’t want to list the tags. This issue is all about URLs – not data display.

    Having said all that.

    The problem still remains. If I, as the admin set the permalinks to use tags in the permalink structure. Then use get_permalink() or the_permalink() – %tag% is not sanitized, which according to the ducumentation, it should be.

    All other aspects pertaining to Friendly-URLs work fine and as expected.

    Thanks again, really appreciate it.

    Thread Starter amidknight

    (@amidknight)

    Try the example below.

    Examples:

    Permanent Link (permalink) structure = /%tag%/%year%/%monthnum%/%postname%

    http://joshbruce.com/tag_url/
    All the recent posts show up – no matter the tag/category – as expected. Clicking on the Permanent Link (permalink) – results in a 400 error (because “%tag%” is literally not replaced with the tag-slug of the lowest number – which is what should happen).

    http://joshbruce.com/tag_url/news
    http://joshbruce.com/tag_url/blog
    Example URL structure (which works) – to isolate posts between the two types based on tags. However, again, get_permalink() does not replace “%tag%” in the link. (It’s not that the post-slug is incorrect – it’s that the delimiter “%tag%” isn’t being replaced.)

    http://joshbruce.com/tag_url/category/photography/flowers
    Categories – being different than tags – works as expected. Only posts with the category photography->flowers show up – independent of tag.

    http://joshbruce.com/tag_url/blog/2009/04/blog-2
    This is an example of a single blog post permalink (the proper post does appear).

    http://joshbruce.com/tag_url/news/2009/04/news-1
    Example of single news post (which, again, proper post does appear).

    Submitting the comment form results in the following:
    http://joshbruce.com/tag_url/%tag%/2009/04/blog-2/comment-page-1#comment-2

    Because the “%tag%” delimiter is not changed.

    Note: With this fresh install – wp_list_comments() works. Not sure if there was a minor patch or anything.

    Therefore, given the way the other parts work as expected & desired – it seems WordPress wants it to be capable of doing something like this. It just doesn’t work with the get_permalink() and the_permalink() functions; or, the redirect for the default comment form (this I should be able to fix with a hidden form element $_POST[‘redirect_to’]).

    Does that demonstrate it a little better?

    ps. Thanks for continuing to look at this – I really do appreciate it.

    Thread Starter amidknight

    (@amidknight)

    Thanks for the information doodlebee I didn’t know that was possible. But, regarding this project, I’m not looking to restrict someone to a category. I’m wanting to create a permalink structure based on tags, and allow commenting (any author can tag a post as “news” or “blog” or whatever). And, the URL a user puts in – will have a tag in it.

    Because this option is available as part of the permalink structure menu, and is treated similar to categories (without parents) – I would expect WordPress to decipher the URL in the same manner it would categories.

    If my permalink structure was something like:

    /%category%/%year%/%title%

    get_permalink() – should return the /category/2009/title URL, yeah?

    wp_list_comments() – should return the comments for /category/2009/title URL, yeah?

    Also, the comment form would redirect to /category/2009/title URL, yeah?

    If the above are true – it should work with tags as well (currently it does not). Otherwise, why have the option available as part of the permalink structure?

    Basically, when it comes to tags – I don’t think WordPress is properly converting the URL when there is a tag involved in the permalink structure – for these particular functions. Beyond that – it works as expected – and the client is pleased.

    Will try to setup a ‘dummy’ site to demonstrate better, because I must not be communicating this well.

    Thread Starter amidknight

    (@amidknight)

    I don’t usually go for bumping. And, normally I would contact the vendor directly.

    Having said that: I need to know if this is a planned “fix” in a soon-to-be-released update? Or, if I should just abandon tags and convert back to the category method?

    have_comments(): returning false. Because the comment count (where there are comments) is greater than 0 in the DB. It would be my guess that the proper ID or GUID is not happening.

    wp_list_comments(): return empty. Same guess as for reason.

    Seems odd to me that comments can be posted. Further, comments are associated with the proper post (front and back end). Also, comments_number() finds the proper count.

    However, the proper default redirect_to part of the form does not work. Finally, the the_permalink() does not take out %tag% (I can work around this one but not very elegantly).

    Again, if someone could let me know – I would appreciate it greatly.

    Thread Starter amidknight

    (@amidknight)

    Basically, the idea is similar to what WordPress MU does – with multiple blogs/users. However, instead of multiple ‘Blogs’ – there are multiple ‘dynamic’ areas to post – while still having the normal category and pagination capabilities of WordPress Posts available (which aren’t really available for Pages – at least that I’m aware of).

    Tags:
    blog
    news
    podcasts
    …etc.

    URLs:
    wpurl.com/blog/
    wpurl.com/news/
    wpurl.com/poscasts/
    wpurl.com/etc/

    This way when an author creates a post – they can tag it as one of those. Then via query_posts(‘tag=tagname’); single out the posts for that URI – a page can be displayed and filtered.

    In theory, an author could tag something as a ‘blog’ then categorize it as ‘technology’. And, another post can be tagged as ‘news’ then categorized as ‘technology’. Finally, an end-user could go to wpurl.com/categories/technology/ and see both Blog and News posts dealing with technology.

    It just seems that the current release hasn’t been fully implemented to use %tag% as part of the Permalink structure – despite the option being available.

    Thread Starter amidknight

    (@amidknight)

    the_permalink(); function also doesn’t convert the %tag% to the proper tagname

    Workaround for permalink:
    <?php print(preg_replace('/%tag%/', 'blog', get_permalink())); ?>

    Thread Starter amidknight

    (@amidknight)

    I went with Tags to differentiate between two organizational methods (previous to this version of WordPress I would have used categories). And, because WordPress uses tags similar (at least for what I need) to categories – I figured it would work the same way.

    Tags = Post Type (blog, news, etc.)

    Categories = Content Focus (flowers, photography, etc.)

    This way instead of creating a parent category called Blog – with child categories (flowers, photography, etc.). Which would in-turn mean creating another parent category called News – with child categories (flowers, photography, etc.).

    I guess you could say I’m part of the “there’s a difference between a tag, and a category” movement. Good and completely valid question.

Viewing 11 replies - 1 through 11 (of 11 total)