Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Parsing variables in WordPress permalinks WordPress



I want to parse a fixed variable in my permalinks.

but I'm currently using pretty permalinks like this.

I'm using categories, and few sub categories under them. so sometimes the permalinks can be like this too !

This is my current permalink structure.

Can someone help me to parse a single variable in this permalink so I can read that in single.php

for example<strong>test-</strong>the-post-name.html


Notice the word, "test".. that is the only variable I want to parse and read later in single.php.

PS : all links, variables here are just for an example..but I will be using this for a site which already have more than 15,000 posts.

Please do help me.

I've seen this problem is solved here,
I recommend you to read this question and just modify his code so it will be easy for you. But iIm not sure if it supports wordpress 3.1
Just modify and add the permalink support to this :

now his coding uses : /%post_id%-%postname%/
(I think the rewrite_rules.php is the only file you have to modify)

I'm using wordpress 3.1 with W3 Total Cache plugin enabled.

Answers (2)


Denzel Chia answers:


Not sure if you are referring to this.

Put this in your single.php

If there is "test" in url it will print "The word test was found"

//Use this function to get current url

function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
} else {
return $pageURL;

$current_url = curPageURL();

if (preg_match("/test/",$current_url, $matches)) {
echo "The word test was found";



Denzel Chia comments:

I should have read
and not waste my time here!

Since you "had" the answer, "I recommend you to read this question and just modify his code so it will be easy for you."

Dharshan Venkadesan comments:

Thanks for your reply.

But when I add any variable to the URL the wordpress is automatically redirecting to the original permalink.

Dharshan Venkadesan comments:

Thanks for all answers.
But I fixed it myself by using this coding in rewrite_rules.php

$rr_rewrite_rules = array(

'([0-9]+)/(.+?)/([^/]+)/test.html(/[0-9]+)?/?$' => 'index.php?mystring=play&p=$matches[1]&category_name=$matches[2]&name=$matches[3]&page=$matches[4]'
$wp_rewrite->rules = $rr_rewrite_rules + $wp_rewrite->rules;

Anyways I will choose you as the winner because of your time spent and effort :-)

Dharshan Venkadesan comments:

This is the final one:
// code by WJM ( 776a6d # )
$flush_rules = FALSE;
//Comment this line below to set $flush_rules to false, after the first time of loading a page to improve performance
//$flush_rules = TRUE;

register_activation_hook ( __FILE__, 'rr_flush_rewrite_rules' );
add_action( 'generate_rewrite_rules', 'rr_add_rewrite_rules');
add_filter( 'query_vars', 'rr_add_rewrite_query_vars' );

function rr_add_rewrite_rules($wp_rewrite) {

// FOR PERMALINK STRUCTURE: /%post_id%-%postname%/
$rr_rewrite_rules = array(
'([0-9]+)/(.+?)/([^/]+)/play.html(/[0-9]+)?/?$' => 'index.php?mystring=play&p=$matches[1]&category_name=$matches[2]&name=$matches[3]',
'([0-9]+)/(.+?)/([^/]+)/view.html(/[0-9]+)?/?$' => 'index.php?mystring=view&p=$matches[1]&category_name=$matches[2]&name=$matches[3]'
$wp_rewrite->rules = $rr_rewrite_rules + $wp_rewrite->rules;

function rr_add_rewrite_query_vars($public_query_vars) {
$new_vars = array(
return array_merge($public_query_vars, $new_vars);

add_action( 'template_redirect', 'rr_disable_canonical', 9);
function rr_disable_canonical() {
if ( get_query_var( 'mystring' ) )
remove_action('template_redirect', 'redirect_canonical');

function rr_flush_rewrite_rules() {
global $wp_rewrite;
if ( $flush_rules == TRUE )
add_action( 'init', 'rr_flush_rewrite_rules');



Rashid Aliyev answers:

WHy You not want to use sub page structure?

Dharshan Venkadesan comments:

Thanks..but I just want this code.. As I said before these are just for an example of usage.. I will be using this for another purpose.

Dharshan Venkadesan comments:

Did u meant the pages by "sub pages" ?
page/3 like that ?

if you can give me a help page about this.