-1

Using this the header is already created on page load, I want all non-login user to redirect to the main website link.

[21-Feb-2017 21:20:40 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /mnt/stor1-wc2-dfw1/462870/524120/sandata.didit-dev.com/web/content/wp-content/themes/Joints Theme/header.php:3) in /mnt/stor1-wc2-dfw1/462870/524120/sandata.didit-dev.com/web/content/wp-includes/pluggable.php on line 1179

include_once(ABSPATH . 'wp-includes/pluggable.php');

function hook_diditdev(){
    if(!is_user_logged_in()){
        wp_redirect( get_option('live_site_link') ); exit();
    }
}
add_action('wp_head', 'hook_diditdev');
vico
  • 2,152
  • 2
  • 17
  • 38

1 Answers1

1

You could do it earlier in the process, using the "wp_loaded" hook for instance:

function hook_diditdev(){
    if(!is_user_logged_in()){
        wp_redirect( get_option('live_site_link') ); exit();
    }
}
add_action('wp_loaded', 'hook_diditdev');

Alternatively, if you can't get a hook early enough, you can always redirect using javascript... Not as elegant, the page will start to load and then redirect, but sometimes it's your only option:

function hook_js_redirect(){
    if($i_need_to_redirect){
        echo '<script> window.location = "'.get_option('live_site_link').'"; </script>';
        exit();
    }
}

Hope this helps!

nibnut
  • 3,072
  • 2
  • 15
  • 17