• Resolved Yaya

    (@ykhaled)


    I made a basic custom page template custom_page.php saved under twentytwentyone/custom_page.php. The page should be available to logged-in users only and it is starting with something like this:

    <?php /* Template Name: custom_page*/ ?>
    <header>
    <?php if (!is_user_logged_in()){
            auth_redirect();
    }
    ?>

    This custom page template was working fine for earlier version of WP. Currently using the latest from the docker hub.

    Checking PHP logs I noticed the following warning:

    PHP Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/themes/twentytwentyone/custom_page.php:2) in /var/www/html/wp-includes/pluggable.php on line 1332

    Developer tools is not showing any errors at all (Chrome, Firefox and Edge).

    Using WP 5.7.1.

    The custom page template has other PHP scripts and JS in the body/header,

    The goal is to restrict the custom page template to logged-in users and redirect them to login if they are not.

    • This topic was modified 3 years, 6 months ago by Yaya.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Your code is outputting blanks and <header> before checking the user. That’s why you get Cannot modify header information.
    Move the check up into the first PHP tag.

    Thread Starter Yaya

    (@ykhaled)

    Thanks @joyously. In deed this fixed my problem. I moved it up earlier together with separate PHP tags but didn’t work. Only if in the same PHP tag as you suggested works.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘auth_redirect in custom page template not directing to login at all’ is closed to new replies.