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!