Make WordPress Core

Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#14383 closed defect (bug) (worksforme)

Custom Permalink Structures Seem To Have Broken

Reported by: truthmedia's profile truthmedia Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Rewrite Rules Keywords: rewrite, permalink, custom permalink, custom rewrite rules
Focuses: Cc:

Description

Up until WordPress 3, we have been running some code in one of our plugins which changes how WordPress reads tag URL's. Instead of the standard /tags/oranges/ type structure, we've been using something along the lines of /s/37/tags/oranges/ which allowed us to split the site into multiple sections. (for example, the above might show different posts than say /s/25/tags/oranges/)

The code we were using seemed to be working fine up until WordPress 3.0. I have included it below. It is an adapted form of the example found near the bottom of this page of Ryan Boren's Feed Director plugin:
http://codex.wordpress.org/Function_Reference/WP_Rewrite

The code we've been using:

add_action('generate_rewrite_rules', 'sectioncats_add_rewrite_rules');
function sectioncats_add_rewrite_rules( $wp_rewrite )
{
	$new_rules = array(
		'[Ss]/([0-9]+)/tags/([^/]+)/?$' => 'index.php?' .
			'tag=' . $wp_rewrite->preg_index(2) .
			'&section_id=' . $wp_rewrite->preg_index(1),

		'[Ss]/([0-9]+)/tags/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?' .
			'tag=' . $wp_rewrite->preg_index(2) .
			'&paged=' . $wp_rewrite->preg_index(3) .
			'&section_id=' . $wp_rewrite->preg_index(1),
	);

	$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

As of WordPress 3, this no longer works. Well... it sorta works in that it keeps the proper section but redirects the browser to /tags/oranges/ (ie back to the default WP structure)

So, my question is, What's the *right* way to do this? I've tried switching to use simple add_rewrite_rule commands, but those don't yield any better results.

Thanks in advance for any help.
James W.

Change History (3)

#1 @truthmedia
15 years ago

  • Cc truthmedia added

Following other examples in the codex, I've tried switching the code to the following, still with the same result.

// Adding new rules
function sectioncats_add_rewrite_rules($rules)
{
	$new_rules = array( 
	
		'[Ss]/([0-9]+)/tags/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?' . 
			'tag=$matches[2]' .
			'&paged=$matches[3]' . 
			'&section_id=$matches[1]',
	
		'[Ss]/([0-9]+)/tags/([^/]+)/?$' => 'index.php?' . 
			'tag=$matches[2]' .
			'&section_id=$matches[1]',
	
	);
	
	$return = $new_rules + $rules;
	
	return $return;
}
add_filter('rewrite_rules_array','sectioncats_add_rewrite_rules');

function sectioncats_query_vars($public_query_vars) {
 
    array_push($public_query_vars, 'section_id');
 
	/*	Note: you do not want to add a variable multiple times.  As in
		the example above, multiple rules can use the same variables 
	*/
 
	return $public_query_vars;
}
add_filter('query_vars', 'sectioncats_query_vars');

#2 @truthmedia
15 years ago

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

Well, apparently I just got it to work. Not sure how.

#3 @scribu
15 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.