TheRealPapa
TheRealPapa

Reputation: 4539

Execute wp_logout_url('/') in Javascript

I want to do a check with JavaScript then based on condition I want to log the user out. How do I call the PHP from JS? Doing research I find lots on info on how to create a logout link, but I am trying to execute it inside JS instead.

Something like:

if(variable == 1) {
   EXECUTE LOGOUT WITH NO CONFIRMATION (in PHP the function to show the link is "<?php echo wp_logout_url('/redirect/url/goes/here') ?>")
} else {                
    DO SOMETHING ELSE HERE;
};

Upvotes: 0

Views: 972

Answers (1)

gherkins
gherkins

Reputation: 14983

Wouldn't simply setting location.href do the trick,
as the URL would be pre-rendered with PHP?

if(  variable == 1  ) {
  window.location.href = '<?php echo wp_logout_url('/redirect/url/') ?>';  
} else {                
    //DO SOMETHING ELSE HERE;
};

Upvotes: 1

Related Questions