0

I'm trying to redirect non-logged in users to the login page when viewing any pages relating to a plugin.

My code is working fine when I specify which pages they can't view, but I'm trying to pull in a string from the URL which is where I'm struggling.

Here is an example URL: http://domain.com/idx/city/irvine/

I am trying to detect the string 'idx' in the URL. I looked at the rewrite rules defined in the IDX plugin I'm using:

"idx/city/([^/]+)(?:/page\-(\\d+))?"       => 'index.php?idx-action=results&idx-q-Cities=$matches[1]&idx-d-ResultPage=$matches[2]'

I've tried checking if $_GET['idx-action'] is set but that didn't work.

// Check for registered user or redirect to login/register page
add_action('template_redirect','tc_check_if_logged_in');
function tc_check_if_logged_in() {
// If user is not logged in, check for restricted pages (inside array) and ignore home/front page and admin pages
//if (!is_user_logged_in() && (is_page( array('1306','999999','999998',)) && !is_home() && !is_front_page() && !is_admin())) // commented out as I know this part works

if ($_GET['idx-action']) // Check if IDX query set 
{
    // If restricted, redirect to login page then redirect back to original page
    $url = add_query_arg(
        'redirect_to',
        get_permalink(),
        site_url('wp-login.php')
    );
    wp_redirect($url);
    exit;
}   
} 

Sorry if question is unclear. Got a really bad head cold and can't think!

2
  • Is there any specific reason you can't just use PHP strpos() to detect it?
    – Nick Young
    Commented May 8, 2014 at 8:06
  • I tried it and it works if there's a page with the term in it (domain.com/idx-test/), but when it's accessing a page generated by the plugin (domain.com/idx/city/irvine/) I just get a white screen (with WP_DEBUG set to true)
    – TomC
    Commented May 8, 2014 at 10:06

1 Answer 1

0

Nothing in $_GET will be set for internal rewrite rules, that's not how they work. Look in the query vars for idx-action- either get_query_var('idx-action') or $wp_query->query_vars['idx-action'].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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