Home WordPress General Functions wp_logout_url() WordPress wp_logout_url() Function Definition In WordPress, wp_logout_url() returns the URL that logs the current user out . The address includes a security nonce , so it only works for the user who is logged in right now.
Pass a $redirect address to choose the page the user sees after logging out.
Syntax It returns a string, so wrap it in esc_url() when you print it.
<?php
echo esc_url( wp_logout_url( home_url() ) ); Call What it does wp_logout_url()The logout address. wp_logout_url( home_url() )Log out, then go to the home page. wp_logout_url( get_permalink() )Log out, then return to this page.
It builds only the URL. You write the link tag.
Full declaration wp_logout_url( string $redirect = '' ): string Parameters Parameter Description $redirectstring. The page to open after logging out. Default is empty.
Return Value Return Value Description string The logout URL with a nonce, and redirect_to when you pass one.
WordPress Examples Three short theme examples. Each one shows the code and what it prints.
1. A simple logout link 2. Go home after logout 3. Add the link to a menu Example 1 — A simple logout link Show it only to logged-in users.
<?php if ( is_user_logged_in() ) : ?>
<a href="<?php echo esc_url( wp_logout_url() ); ?>">Log out</a>
<?php endif; ?> Logged in:
<a href="https://example.com/wp-login.php?action=logout&_wpnonce=abc123">Log out</a>
Guest:
(nothing is printed) How It Works is_user_logged_in() hides the link from guests.wp_logout_url() returns the address with a nonce.esc_url() makes it safe for HTML.Example 2 — Go home after logout Send the user to the home page.
<a href="<?php echo esc_url( wp_logout_url( home_url() ) ); ?>">Sign out</a> <a href="https://example.com/wp-login.php?action=logout&redirect_to=https%3A%2F%2Fexample.com&_wpnonce=abc123">Sign out</a> How It Works The first argument is the next page. It is added as redirect_to. After logout the user lands on the home page. Frequently Asked Questions What does wp_logout_url() return? It returns a secure URL that logs the current user out.
Why does the URL have _wpnonce? The nonce is a security check. It stops other sites from logging a user out without permission.
How do I choose the page after logout? Pass the address as the first argument, for example wp_logout_url( home_url() ).
Does it print a link? No. It returns only the URL. Use wp_loginout() if you want a ready-made link.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion