Bob Lozano
Bob Lozano

Reputation: 592

Wordpress - Header() not working

In my header i have this code:

<?php 
global $post;
$pgid = $post->ID;

if (is_user_logged_in() || is_page(344)) { //page 344 is the Login Page
 } else { 

$url = admin_url();

    header("Location: http://cherry.virtek-innovations.com/login-screen");
    die();
     } ?>

This code is supposed to send any not logged in users to the login page, however I get this error:

Warning: Cannot modify header information - headers already sent by (output 
started at /home1/virtek/public_html/cherry/wp-content/themes/twentytwelve/
resumen.php:2) in /home1/virtek/public_html/cherry/wp-content/themes/
twentytwelve/header.php on line 10

This is my code in the resumen.php file

<?php 
get_header(); 

/*

    Template Name: Resumen

*/
?>

I have no idea why it wont work, I'm using User Meta Pro Plugin, does any of you find anything wrong in my code? Or know any other method to have a custom login page that won't let you in unless you login?

Upvotes: 0

Views: 2295

Answers (1)

henrywright
henrywright

Reputation: 10240

You could try using the wp_redirect() function to redirect instead? For example, put this in your theme's functions.php file:

function redirect_logged_out_users() {
    if ( ! is_user_logged_in() ) {
        wp_redirect( home_url() . '/login-screen/', 301 );
        exit();
    }
}
add_action( 'template_redirect', 'redirect_logged_out_users' );

This executes at the point at which the template_redirect hook runs. That is before anything is sent to the browser so you won't get the 'headers already sent' problem with this method.

Ref: http://codex.wordpress.org/Function_Reference/wp_redirect

Upvotes: 1

Related Questions