shortcode - Logout user if click on a custom page link

admin2025-01-07  3

I want to logout a user if the user click on custom page link (e.g. example/logout). I need to do that because my site users are not allowed to access wp-login.php page. I've a front-end login page but don't have logout page.

How can I make custom page as logout link? With or without shortcode.

I want to logout a user if the user click on custom page link (e.g. example.com/logout). I need to do that because my site users are not allowed to access wp-login.php page. I've a front-end login page but don't have logout page.

How can I make custom page as logout link? With or without shortcode.

Share Improve this question asked Jul 2, 2019 at 7:44 akarimakarim 3052 gold badges5 silver badges15 bronze badges 1
  • wp_logout_url() returns the link to logout. – Max Yudin Commented Jul 2, 2019 at 8:03
Add a comment  | 

1 Answer 1

Reset to default 0

yes we make custom page as logout link on this way add this in function.php

add_action( 'init', 'log_out_user' );function log_out_user() {
global $wp;
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; //get current page url
if(!empty(get_current_user_id()) && $url == 'example.com/logout' ) //check if user is logged in and current page url
{
    wp_logout(); //this will logout user
    $siteUrl = site_url(); //get site url 
    wp_redirect($siteUrl);//add this if you want to redirect to site url after logout
}}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258911a549.html

最新回复(0)