How to pass query string vars between admin pages?

admin2025-06-03  3

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>";

}

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>";

}
Share Improve this question edited Feb 13, 2019 at 3:58 cag8f asked Feb 12, 2019 at 9:04 cag8fcag8f 1,9973 gold badges21 silver badges31 bronze badges 3
  • Is there a copy and paste error, or are you forgetting to jump back into PHP after the link in cag_reports_page()? – tmdesigned Commented Feb 12, 2019 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 Commented Feb 13, 2019 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 Commented Feb 13, 2019 at 3:58
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748913437a314764.html

最新回复(0)