0

I want to pass two query string variables, and their values, from one admin page to another. When I try to do so, on the final destination page, it indicates the variables are not set. My code is below. Anyone see what I'm doing wrong?

My general logic was to add the query string vars to the WordPress list of query string vars (using the query_var hook). I then set the value of each query string variable, and appended each query string variable to the destination URL (using add_query_arg()). On my destination page, I then tried to access the value of each variable, using get_query_var().

One possible source of error is that my destination URL already has one query string appended to it. I didn't think that would matter, but perhaps it does.

There is nothing in the PHP error log.

There were many previous Stack Exchange questions regarding query string variables, but I didn’t see anything specifically about doing so on admin pages.


function custom_query_vars_filter($vars) {
  $vars[] .= 'location';
  $vars[] .= 'department';
  return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );



function cag_reports_page(){
    $params = array('page' => 'user-summary','location' => 'san-francisco', 'department' => 'design');
?>
    <a href="<?php echo add_query_arg($params, '/wp-admin/admin.php'); ?>">My Link</a><?php
}


function user_summary_page(){

  $location = get_query_var('location', 'not set');
  $department = get_query_var('department', 'not set');

  echo $location . "<BR>";
  echo $department . "<BR>";

}
  • Is there a copy and paste error, or are you forgetting to jump back into PHP after the link in cag_reports_page()? – tmdesigned Feb 12 at 10:48
  • @tmdesigned I see what you're talking about. I'll double check on that. But if that is actual code, wouldn't it result in a fatal error? If so, I can confirm no fatal error. But let me just check. Give me a couple hours. – cag8f Feb 13 at 3:13
  • @tmdesigned I checked, and yes, that was just a copy/paste error. I've edited my original post. Thoughts on what's not working, or how I could troubleshoot this? – cag8f Feb 13 at 3:58
1

I did some more nosing around in Stack Exchange, and found this previous question. it has no accepted answers, but a few of the replies gave specific instructions on how to pass variables to admin pages (the approach is different than doing so on front-end pages). I tried the $_GET[] method, and it worked.

  • 1
    Good work. Didn't know there would be such a difference. I guess it makes sense. Learn something new every day. – tmdesigned Feb 13 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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