In my WordPress website I tried to log out user with wp_logout(), and redirect them to the login page with wp_redirect(); method. But wp_redirect() not redirecting user to supplied URL

Logout and redirect code:

wp_redirect( 'http://localhost/wordpress/wp-login.php?loggedout=true' ); 

I tried to use wp_logout and wp_redirect hooks, they didn't work either

add_action ('wp_logout','logout_user');
        function logout_user()
            wp_redirect( 'http://localhost/wordpress/wp-login.php?loggedout=true' );
  • 1
    Are you sure nothing else is being output beforehand? Redirects need to happen before the headers have been sent out, so if anything has been output already, it won't work and you'll get a PHP warning. Check your error logs – Tom J Nowell Jul 9 '18 at 20:27
  • Yes. Nothing is being printed before. I made my repository public and you can access the file here github.com/pavankjadda/wordpress/blob/wordpress_login/… – Jadda Jul 10 '18 at 16:02
  • That's not true, when I say nothing can be output i mean absolutely nothing of any kind, not just human visible stuff. I can see opening div containers and other output, or it to work it has to come before everything, including the get_header call, right at the top. The moment PHP sees even a single character, say ` ` a white space character, it sends out headers telling the browser to expect HTML, and your opportunity to send redirect headers is gone – Tom J Nowell Jul 10 '18 at 16:06
  • Additionally, by the time that template is loaded, template_redirect has already happened. It needs to go inside your functions.php or a plugin. The order things run matters here. Also all your changes will get overwritten when twentyseventeen gets updated, you should move all of this into a child theme so that doesn't happen – Tom J Nowell Jul 10 '18 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.