Hi there
I posted the following in the 'themes' forum a few days ago, but I got no replies and just realised that what I am doing here probably falls more into the remit of this forum. I am absolutely desperate to fix this problem; I thought it had resolved itself (as the stack exchange question indicates), but it is back again.
Original thread - mods feel free to delete it: http://wordpress.org/support/topic/authentication-cookie-not-always-recognised-by-my-theme?replies=1
I am currently developing a custom WordPress theme for my rugby team. I am trying to create a members area for my site using custom post types and the following code included into my functions.php:
<?php
function redirect_restricted_areas( $query )
{
if ( !is_admin() && $query->is_main_query() )
{
if ( ! current_user_can( 'view_players_area' ) && (
$query->is_singular ('player-page') ||
$query->is_post_type_archive ('player-page')
) )
header('Location: ' . wp_login_url( "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" ) );
}
}
add_action ( 'pre_get_posts', 'redirect_restricted_areas' );
The problem is that it works, but NOT consistently. If I click login, it redirects to the login page and then back to my members area where I am now logged in. If however, I am not logged in and I type url/players-area/ into the browser, more often than not it redirects me to login.php and then BACK there when I am logged in. If I then type /wp-admin/ into the browser, the dashboard works fine.
I have documented this problem in more detail here.
I would *really* appreciate some help with this matter as soon as is possible because I am at my wits end here.