• Resolved themarco

    (@themarco)


    Ok this is driving me up the wall. I hope someone can help.

    I’m trying to fix a plugin and I need some help.
    I have set up a page, let’s say it’s /mypage/

    Now, I want to use a custom parameter in the URL in the plugin code used in that page. This means I want an URL like /mypage/marco to result in the plugin code to get access to a variable containing the ‘marco’.

    I have searched the web like crazy and looked all over the sparse documentation about the internal URL rewriting mechanism but I just can’t seem to figure it out which is highly frustrating.

    SO:

    How do I make a variable, say $myvar available containing the earlier described ‘marco’ string on a page located at /mypage/ ?

    Any insights are highly appreciated.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Can’t you just use the GET or the POST methods? I think they will work smoothly in wordpress (even with friendly permalinks).

    Thread Starter themarco

    (@themarco)

    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.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    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.

    Thread Starter themarco

    (@themarco)

    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');

    bughy

    (@bughy)

    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 (https://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!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘using an extra parameter in an URL’ is closed to new replies.