0

I have a question and a technical issue in wordpress. That is, i have a login form on my website to allow visitors to login to see special content. The question goes here, when the time they log in, i am managed to redirect them to a targeted page. When the time they logging out from the member zone, i am out of idea how to send them to a different targeted page.

In my function.php file, I have this code:

add_action('wp_logout',create_function('','wp_redirect(home_url());exit();'));

How to add the page url that i want to redirect them to this code above? I am just beginner in php. Hope to get some guidance.

3 Answers 3

2

home_url() will accept a parameter that will be used in the creation of the URL.

The bare minimum solution would be:

add_action(
  'wp_logout',
  create_function(
    '',
    'wp_redirect(home_url("/path/to/page"));exit();'
  )
);

site_url() will also accept the same parameter and may be (probably is) a better choice.

I believe that using either of those will make the link dependent on permalink settings. I don't think that those function will translate between different settings, though I have not tested that.

I would suggest using get_permalink() with an explicit page/post ID.

add_action(
  'wp_logout',
  create_function(
    '',
    'wp_redirect("'.get_permalink(1).'");exit();'
  )
);

Note that you don't need home_url() or site_url() since get_pemalink() will generate the complete URL.

3
  • Hi You got it work now. Thank you very much! I will remember this guidance. Thank you! =)
    – Jornes
    Aug 11, 2013 at 17:28
  • 1
    While it is a good idea to remember, you would want to accept the answer, if it was helpful to you. Aug 11, 2013 at 17:36
  • Johannes Pille, excuse me! I did not know about this. But i just did it. Thank you!
    – Jornes
    Aug 14, 2013 at 15:20
0

First i think will be better to

add_action('wp_logout',create_function('','wp_redirect(home_url());exit();'));

Can instead be replaced to look like this

function wpse_109694(){
   $redirect_to = home_url();
   wp_redirect($redirect_to);
   exit();
}

add_action('wp_logout', 'wpse_109694');

Once this change is done , you will see that to redirect to another page of your choice , you just edit the $redirect_to variable to the url of the page you need.

3
  • Hansy Schmitt, thank you for your answer, too! But, i am wondering what is the code 'wpse_109694' for? Is that code redirecting user back to the current page after they have logged out? Hope to get your answer soon.
    – Jornes
    Aug 14, 2013 at 15:23
  • Actually is the same code you used, but i expanded so it looked easier for you to understand. In this case , you should only change the variable name $redirect_to to match where you want the page to be redirected ( for example , if we take s_ha_dum sample , i would use $redirect_to = get_permalink(1) or $redirect_to = home_url("/path/to/page") )
    – hacksy
    Aug 14, 2013 at 17:21
  • Lol...i see! Hansy, i tried to use .get_permalink(1) in the code, but it redirected me to a post page instead of previous page where i was. I find no way how to fix that then i have created a logout page where they will be redirected after logged out.
    – Jornes
    Aug 14, 2013 at 18:49
-1
add_action('wp_logout',function(){wp_redirect(home_url());exit();});
1
  • Please, explain what the code does and how it answers the question. Just dumping code is not a good answer.
    – cybmeta
    Oct 20, 2017 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.