I am creating a membership site where I am trying to login a particular user from a link sent to their email address.
I have a custom post typed called "Members", where I have stored a unique key with the name "user_key". After a particular process, the email is sent to that user with an auto login link.
An example link looks like the following
http://example.com/process/?key=DcP2K7cmBUrLVRjizs78RAuuoMGRFc6F6TMDm6E6
Process is the page which handles the login part. Here is the code used in that page.
get_header();
$key = $_GET['key'];
$args = array(
'post_type' => 'member',
'meta_value' => $key
);
$properties = new WP_Query($args);
if($properties->have_posts()) :
while($properties->have_posts()) : $properties->the_post();
$author = $post->post_author;
$user = get_user_by('ID', $author);
$user_id = $user->ID;
echo $user_id;
wp_set_current_user($user_id, $loginusername);
wp_set_auth_cookie($user_id);
do_action('wp_login', $loginusername);
wp_redirect( home_url() );
endwhile;
endif;
wp_reset_query();
get_footer();
When I go to that particular link, I get the following error.
"Warning: Cannot modify header information - headers already sent"
From what I understand, I think it's the "wp_set_auth_cookie" part which is done after the headers are loaded, which is creating that problem.
Anyone has any ideas how to fix it?