Hi,
I want to parse a fixed variable in my permalinks.
but I'm currently using pretty permalinks like this.
http://www.example.com/15454/pictures/funny/the-post-name.html
I'm using categories, and few sub categories under them. so sometimes the permalinks can be like this too !
http://www.example.com/15454/pictures/funny/animals/cats/the-post-name.html
This is my current permalink structure.
/%post_id%/%category%/%postname%.html
Can someone help me to parse a single variable in this permalink so I can read that in single.php
for example
http://www.example.com/15454/pictures/funny/animals/<strong>test-</strong>the-post-name.html
OR
http://www.example.com/15454/pictures/funny/animals/<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.
Thanks.
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 :
/%post_id%/%category%/%postname%.html
now his coding uses : /%post_id%-%postname%/
http://wpquestions.com/question/show/id/618
(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.
Denzel Chia answers:
Hi,
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"
<?php
//Use this function to get current url
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$current_url = curPageURL();
if (preg_match("/test/",$current_url, $matches)) {
echo "The word test was found";
}
?>
Thanks
Denzel Chia comments:
I should have read http://wpquestions.com/question/show/id/618
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:
<?php
// code by WJM ( 776a6d # gmail.com )
$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(
'mystring',
);
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');
return;
}
function rr_flush_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
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/2
page/3 like that ?
if you can give me a help page about this.
Thanks.