• Hello,

    I have set a page as the home page of my site in settings -> reading.

    I am trying to redirect users if they are logged and on the home page to a different page using this code:

    add_action(‘init’, ‘redirect_if_user_logged_in’);
    add_action('template_redirect', 'redirect_if_user_logged_in');
    function redirect_if_user_logged_in() {
    	
        if ( is_front_page() && is_user_logged_in() ) {
            wp_redirect('(my url here)'); 
         exit;
       }
    	
    }

    but my php won’t save because it will give a fatal error “Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.”

    I tried using is_home() instead but that doesn’t redirect. Can someone please tell me what I’m doing wrong?

    • This topic was modified 3 years, 6 months ago by vampy123.
Viewing 1 replies (of 1 total)
  • Hi, vampy123, you need to remove the first line in your code:

    add_action(‘init’, ‘redirect_if_user_logged_in’);

    As https://codex.www.ads-software.com/Conditional_Tags states:

    Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.

    Since you’re using conditional tags, they won’t be accessible in the init hook.

Viewing 1 replies (of 1 total)
  • The topic ‘trying to redirect with is_front_page() gives fatal error’ is closed to new replies.