WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 8 years ago

#4652 closed defect (bug) (duplicate)

Page and category trailing back-slash dependent on post permalink structure

Reported by: snakefoot Owned by:
Milestone: Priority: high
Severity: major Version: 2.2
Component: General Keywords: wp_rewrite
Focuses: Cc:

Description

If specifying a post permalink structure like this:

/article/%postname%.html

Then WP_Rewrite will discover that there is no ending back-slash and will set use_trailing_slashes to false.

Whenever creating links to categories og pages then it will now remove the ending back-slash.

This changes all category and page links when upgrading from 2.0 to 2.2, with duplicate content to follow.

The quick patch to this problem is to create a plugin with the following lines:

<?php
function fix_trailing_backslash() {
  global $wp_rewrite;
  $wp_rewrite->use_trailing_slashes = true;
}

function fix_trailing_backslash_post( $link ) {
  global $wp_rewrite;
  if (!substr($wp_rewrite->permalink_structure, -1, 1) == '/' ))
    return rtrim( $link, '/' );
  return $link;
}

add_action('init', 'fix_trailing_backslash');
add_filter('post_link', 'fix_trailing_backslash_post' );
?>

Change History (6)

comment:1 @snakefoot8 years ago

Ofcourse I have to make an error in the plugin code :)

<?php
function fix_trailing_backslash() {
  global $wp_rewrite;
  $wp_rewrite->use_trailing_slashes = true;
}

function fix_trailing_backslash_post( $link ) {
  global $wp_rewrite;
  if (!substr($wp_rewrite->permalink_structure, -1, 1) == '/' )
    return rtrim( $link, '/' );
  return $link;
}

add_action('init', 'fix_trailing_backslash');
add_filter('post_link', 'fix_trailing_backslash_post' );
?>

comment:2 @snakefoot8 years ago

The wrong parantes ofcourse, hopefully the final fix now :)

<?php
function fix_trailing_backslash() {
  global $wp_rewrite;
  $wp_rewrite->use_trailing_slashes = true;
}

function fix_trailing_backslash_post( $link ) {
  global $wp_rewrite;
  if (! (substr($wp_rewrite->permalink_structure, -1, 1) == '/') )
    return rtrim( $link, '/' );
  return $link;
}

add_action('init', 'fix_trailing_backslash');
add_filter('post_link', 'fix_trailing_backslash_post' );
?>

comment:3 @snakefoot8 years ago

Ok just discovered that user_trailingslashit has a nice filter also, much prettier plugin now:

<?php
function fix_slash($string, $type) {
  global $wp_rewrite;
  if ($wp_rewrite->use_trailing_slashes==false && $type != 'single')
    return trailingslashit($string);
  return $string;
}

add_filter('user_trailingslashit', 'fix_slash', 66, 2 );
?>

comment:4 @foolswisdom8 years ago

  • Milestone changed from 2.2.2 to 2.3 (trunk)

How does this relate to #3899 and #4554 (if at all)?

comment:5 @snakefoot8 years ago

It is dup of #3899.

Apparently the solution to this issue is writting a plugin (like the one I posted after a few attempts). Guess the install guidelines for WP 2.2 should be updated, so one is made aware of the change in Permalink-links and that one should install a plugin if using a post permalink structure without trailing slash.

comment:6 @Nazgul8 years ago

  • Milestone 2.3 (trunk) deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #3899.

Note: See TracTickets for help on using tickets.