0

I have a custom Wordpress login and if the user gets to the homepage and is not logged in, they are redirected to the custom login form(I have disabled this for now while I test). What I would like to do is create a URL parameter so that certain users can still get to the homepage without having to log in. From my research it seems like this is possible with a custom query_var. Right now I am just trying to make it work with anything entered into the new query_var and then once I get that working I will make sure it matches a specific value. Here is what I added to my functions.php:

add_action('init','add_var');
function add_var() { 
  global $wp; 
  $wp->add_query_var('auth');
}
if (get_query_var('auth')) {
  echo 'User is visiting from approved URL';
}

I am then adding ?auth=true to the homepage URL, but this does not seem to be working. Nothing is being echoed out. Any ideas?

  • 1
    get_query_var() won't work if you call it from the root of the functions file like that. You should use later hooks like parse_query or wp. – Sally CJ Oct 25 '19 at 0:54
  • But one would add a custom query var for rewriting purposes or when the var is to be used with the WP query/request. Otherwise, you could simply use $_GET['auth'] which works anywhere in a PHP file. – Sally CJ Oct 25 '19 at 1:05
0

The get_query_var() function and/or method only works with registered WordPress query_vars. You can actually view the method via get_query_var().

The normal URL parameters you can access with the normal PHP super global $_GET.

| improve this answer | |

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.