Can’t you just use the GET or the POST methods? I think they will work smoothly in wordpress (even with friendly permalinks).
Not sure what you mean here. When I load /mypage/marco, WordPress redirects back to /mypage/. There seems to be no way to have the extra ‘marco’ bit and read it in plugin code. Well that is, there probably is one but I can’t seem to understand the whole rewrite voodoo in WP.
You need to know about the query_vars, I think. This should all be in a plugin, probably.
To add a query variable:
add_filter('query_vars', 'add_my_var');
function add_my_var($public_query_vars) {
$public_query_vars[] = 'some_unique_identifier';
return $public_query_vars;
}
You can check for that identifier in your code like so:
$myvar = get_query_var('some_unique_identifier');
And there you go. Now you can call the main WordPress URL with ?some_unique_identifier=myvalue.
But, you want it pretty, right? To add a rewrite rule to WordPress:
add_rewrite_rule('mypage/([^/]+)/?$', 'index.php?page_id=PAGE_ID_OF_MYPAGE&some_unique_identifier=$matches[1]','top');
Something like that should do the trick, I think. I’m not 100% with the rewrite rule thing.
Thanks a lot Otto, this was extremely helpful! In the hope that it will help others here’s my final working solution:
function add_my_var($public_query_vars) {
$public_query_vars[] = 'myvar';
return $public_query_vars;
}
add_filter('query_vars', 'add_my_var');
function do_rewrite() {
add_rewrite_rule('mypage/([^/]+)/?$', 'index.php?pagename=mypage&myvar=$matches[1]','top');
}
add_action('init', 'do_rewrite');
Where should I add that code? What file should I use and what section?
I have a plugin installed that sets the URL’s like this: wordress/pagename/?var=xt
i want it to be like:
wordpress/pagename/xt
Where wordpress is the wordpress base (http://domain.com/wordpress/)
Just wanted to thank this thread for solving my problem (thread), I just wish is was easier to find….
I am new to WordPress. I am trying to able to modify the labels on pages to some other language via a script that I have created which holds the translated text. The idea is that if the url has a parameter such as language=French the right text is displayed. I have done the script and tested it and works ok. The problem I have is how to inject this parameter to the links that are on WordPress pages so that when a user selects a particular language, then whichever link they click, it should have this parameter. Does anyone have an idea of how I can do this?
Thank you very much! This helped out a lot, but I have one related question. How would I go about doing the same thing except with more variables? Taking Marco’s example:
“mypage/marco” should direct the user to marco’s main page
“mypage/marco/comments” should direct the user to marco’s comments page
“mypage/marco/friends” should direct the user to marco’s friends page
If anyone can shed some light on this problem it would be much appreciated.
How do i pass the second variable like
index.php?pagename=mypage&myvar=$matches[1]&myvar2=$matches[2]
“mypage/country/city/”
Have tried now for many hours with no result..
I’m also struggling for a long time now with adding a second parameter… does anyone has the answer yet?
Looking forward to new posts with solutions!