Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding an if/else PHP redirect to a page template in Wordpress

Okay, so here's a puzzler: Wordpress, as we all know has a redirect function that looks something like this:

<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>

So what I'm looking to make is a page template that can be applied to specific secure pages, so that when an individual who is not logged in tries to access those pages, they get redirected to the wp-login page. Simple, right? Not so much. Here's my code:

<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
wp_redirect( 'http://www.visionarywomanhood.com/qcgroup/wp-login.php', 301 ); exit;
}
?> 

The "echo" part is in there just to make sure the code is working properly; and sure enough, when I load the page when I'm logged in, up in the corner of the page is 'Welcome, registered user!'.

The problem comes when I'm not logged in. All I get is a white screen; no redirect. The weird thing is when I put in:

<?php wp_redirect( 'http://www.example.com', 301 ); exit; ?>

...at the top of the page, the redirect works like a charm. What on earth is the difference, and what should I do to make this work??

like image 930
user1709061 Avatar asked Jan 27 '26 14:01

user1709061


2 Answers

This is probably happening because the page headers have already been sent.

Use this instead (place in your functions.php):

function auth_check(){
    if ( !is_user_logged_in() && is_page_template('your_template_name.php')) {
        wp_redirect( 'http://www.visionarywomanhood.com/qcgroup/wp-login.php', 301 ); exit;
    }
}
add_action('init', 'auth_check');

It will only execute when the user isn't logged in and when they're accessing a page using your template.

If this isn't an option then use:

if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
   echo "<script>window.location.href = 'http://www.visionarywomanhood.com/qcgroup/wp-login.php';</script>";
   exit();
}
like image 170
Joe Avatar answered Jan 30 '26 05:01

Joe


PHP redirects must be called before any HTML (or anything at all) is outputted. This means, you can't use them in the middle of the page as you see fit.

That means you could have one space somewhere in a PHP file that would make it impossible for you to redirect, and it might be hard to find it.

Alternatively, you can do a Javascript redirect at this point.

From the PHP documentation (http://php.net/manual/en/function.header.php) :

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Also, here is an example of how you would redirect with Javascript :

<script>window.location.href = 'http://www.visionarywomanhood.com/qcgroup/wp-login.php';</script>"

Note that it would not work with all users, since some of them could have disabled Javascript.

like image 23
Dany Caissy Avatar answered Jan 30 '26 03:01

Dany Caissy