Opened 4 years ago

Closed 3 years ago

#9466 closed defect (bug) (wontfix)

Drop %tag% support in permalinks

Reported by: gashek Owned by:
Priority: low Milestone:
Component: Permalinks Version: 2.7.1
Severity: minor Keywords: needs-patch
Cc:

Description

function get_permalink($id = 0, $leavename = false) not support %tag%

My variant this function:
function get_permalink($id = 0, $leavename = false) {

$rewritecode = array(

'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
$leavename? : '%postname%',
'%post_id%',
'%category%',

'%tag%',

'%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);

$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;

}

}


add tag to permalink
$tag = ;

if ( strpos($permalink, '%tag%') !== false ) {

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

if ( !empty( $tags ) ) {

usort($tags, '_usort_terms_by_ID'); order by ID

$tag = $tags[0]->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,
$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);

}

}

Attachments (1)

9466.diff (869 bytes) - added by DD32 4 years ago.
gashek's code in diff form

Download all attachments as: .zip

Change History (31)

comment:1 follow-up: ↓ 3   hakre4 years ago

  • Resolution set to invalid
  • Status changed from new to closed

it does not support %tag% because there is and is no place for %tag%. that is simply bogus.

additioanlly: please chekcout on how to upload patches. Wordpress uses SVN, get SVN and create a patch. upload the patch. .patch files are automatically highlihgted so that other developers see, what has been changed.

thanks.

comment:3 in reply to: ↑ 1   gashek4 years ago

  • Resolution invalid deleted
  • Status changed from closed to reopened

Replying to hakre:

it does not support %tag% because there is and is no place for %tag%. that is simply bogus.

http://codex.wordpress.org/Using_Permalinks - Choosing your permalink structure

"%tag%

A sanitized version of the tag name (tag slug field on New/Edit Tag panel)."

but it does not work, probably a mistake

  • Resolution set to invalid
  • Status changed from reopened to closed

the function is about POSTS, not TAGS: (quote from docs:) Retrieve full permalink for current post or post ID.

the link you provide ( http://codex.wordpress.org/Using_Permalinks ) states it clearly as well: this link is about the UI, the ACP, the ADMIN, the BACKEND, it has nothing to do with the function in wordpress core you reference.

still the same, still close. feel free to re-open but please provide something seriously. forexample that that function is not properly working with IDs of a Post.

comment:5   DD324 years ago

  • Keywords needs-patch added; permalink tag removed
  • Milestone changed from Unassigned to 2.8
  • Resolution invalid deleted
  • Status changed from closed to reopened
  • Summary changed from Not work %tag% in permalinks to %tag% not handled properly in get_permalink

The posters request is valid.

Currently the Permalink Documentation specifies that %tag% along with, %category% are valid within the Permalink structure.

WP_Rewrite is aware that %tag% is valid(see http://core.trac.wordpress.org/browser/trunk/wp-includes/rewrite.php#L611), However, get_permalink() is not(see http://core.trac.wordpress.org/browser/trunk/wp-includes/link-template.php#L81 ), So it returns a bad permalink when %tag% is used. (It doesnt get replaced)

DD324 years ago

gashek's code in diff form

comment:6   DD324 years ago

  • Keywords has-patch added; needs-patch removed

attachment 9466.diff added
gashek's code in diff form

Note, That is a post doesnt have a tag, the generated url will be in-accessable.

Note, that Performance is severly affected by the usage of %tag%,%category% or %author% at the start of the permalink structure.

Thanks for clarifying and providing a patch.

  • Keywords needs-testing added
  • Keywords needs-patch added; has-patch needs-testing removed

patch isn't working.

  • Milestone changed from 2.8 to Future Release
  • Priority changed from normal to low
  • Severity changed from normal to minor

punting to future, pending a patch that works

comment:12 in reply to: ↑ 9   DD324 years ago

Replying to Denis-de-Bernardy:

patch isn't working.

...And by that you mean?

If you test something, Report back on whats actually wrong would you???

  • Keywords has-patch tested 2nd-opinion added; needs-patch removed

well, the obvious: when I applied the patch, permalinks had %tag% in them, instead of the actual tag. :-)

I just tested it again, though, and it now seems to work. mmm...

one slight issue I'd have with this one, however, is that, when no tag is present, the /%tag%/%postname%/ permalink becomes title/.

Yep, The blank-item was something i mentioned in my attachment comments.

It'd happen with the category as well if it wasn't for the fact of the default category.

Another option might be to automatically create the %taxonomy-query-arg% when new taxonomies are added.. but that'd need back-compat for categories and possibly tags.. and possibly change the entire way the WP_rewrite/WP_query classes operate..

How about we remove support for any taxonomy other than category from the permalink structure? We shouldn't encourage such things because of performance reasons and because of the lack of a default. %tag% is already broken, so nothing lost.

Hm... Sounds good to me actually. It'll be far better off in the long run.

Should be able to be added by a plugin if anyone actually really wants it too.

the issue is, we'd then need to add the needed filters over at the whether we should use verbose page rules, and so on. we could be certain someone out there will forget to ues them if we do.

If someone makes a plugin to do it, then yes, they'd need to enable verbose rules.

How about removing $this->use_verbose_page_rules = false; explicitly in the check, And only set it to true there. That way, Any plugin could do a GLOBALS[wp_rewrite]->use_verbose_page_rules = true;

suggesting wontfix for this one.

Lets strip the rest of the current %tag% support out and call this fixed that way instead.

  • Keywords needs-patch added; has-patch tested removed
  • Milestone changed from Future Release to 2.9
  • Owner changed from ryan to Denis-de-Bernardy
  • Status changed from reopened to accepted

ok

  • Summary changed from %tag% not handled properly in get_permalink to Drop %tag% support in permalinks
  • Keywords 2nd-opinion removed

+1 for stripping %tag% support out of the permalinks. the whole permalinks concept needs more genreal consideration first.

  • Owner Denis-de-Bernardy deleted
  • Status changed from accepted to assigned

I believe this is fixed

Fixed as in %tag% support has been dropped from permalinks

  • Resolution set to fixed
  • Status changed from assigned to closed

what johnbillion said.

  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Milestone 2.9 deleted
  • Resolution set to wontfix
  • Status changed from reopened to closed

Setting correct resolution.

Related: #4875 #10786

Note: See TracTickets for help on using tickets.