Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What is the best way to pass arguments to another page in wordpress. I did it this way :

<a href="get_permalinka(id_of_page).'/&i=2&j=3&k=4'">Link/a>

I get this arguments with $_GET['i'],$_GET['j'],$_GET['k'],problem is : this works just with default permalinks,but when I change it to some other permalink type,it does not work any more.Note - I'm passing these arguments from homepage to another page(template page).

Thank you for your time.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Use add_query_arg() to do this.

Here's a useful function if you need to get the current page URL (when get_permalink is inaccesible, like on Archives):

function get_current_page_url() {
  $request = esc_url($_SERVER["REQUEST_URI"]);

  $pageURL = (is_ssl() ? 'https' : 'http').'://';
  if ($_SERVER["SERVER_PORT"] != "80") $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$request; else $pageURL .= $_SERVER["SERVER_NAME"].$request;

  if (false === strpos(get_option('home'), '://www.')) $pageURL = str_replace('://www.', '://', $pageURL);
  if (false !== strpos(get_option('home'), '://www.') && false === strpos($pageURL, '://www.')) $pageURL = str_replace('://', '://www.', $pageURL);

  return $pageURL;
}
share|improve this answer
    
Ok,tnx but why $qvar = get_query_var('i'); do not work? –  user1147 Dec 22 '10 at 10:15
    
because you need to register that query variable to wp. see the query_vars filter –  onetrickpony Dec 22 '10 at 10:23
    
What do you mean "when get_permalink is inaccesible, like on Archives"... curious.. –  t31os Dec 22 '10 at 11:50
    
I wrote this : add_filter('query_vars', 'p_query'); function p_query($qvars) { $qvars[] = 'i'; return $qvars; } and it does not work. –  user1147 Dec 22 '10 at 12:04
    
@t31os: I meant the permalink of the current page, not the one from the posts listed on that page; @user1147: post your entire code –  onetrickpony Dec 22 '10 at 12:54
    add_filter('query_vars', 'p_query');

    function p_query($qvars) {
        $qvars[] = 'i';
        return $qvars;
    }

     <a href="<?= add_query_arg('i', '2', get_permalink(some_page_id)); ?>">Link</a>

and on the page I call

$qvar = get_query_var('i');
echo $qvar;

$qvar is empty!

share|improve this answer
    
This should of been added to the original question, unless you're answering(providing a solution) to your own question. –  t31os Dec 22 '10 at 13:26
    
Well it can be a solution to his questions, as the code is correct and works as long this is done on singular type page. If not, use the get_current_page_url() function above instead of get_permalink()... –  onetrickpony Dec 22 '10 at 13:33
    
Part with get_permalink works,on other page where I call get_query_var,that thing do not work? –  user1147 Dec 22 '10 at 13:54
1  
let's clear this thing out - You add the filter in your functions.php; The "Link" can be anywhere (ps: I suggest you use <?php echo instead of <?=); When you click the link it should take your to a page where you can access your query variable (the link and address bar should point to something like site/page/?i=2) –  onetrickpony Dec 22 '10 at 14:02
    
I didn't put my filter in function.php,now I put it and it works –  user1147 Dec 22 '10 at 14:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.