WordPress site in subdirectory, logout function failing

We have WordPress site in the root of our domain. A translation plugin we use appends the domain with (in our case for Czech) /cs – this means we can run more than one translation but use the same database and wp-content as the main English website.

However, the added /cs causes the logout function to fail as it tries to use the current directory as the basis for where the actual site content is being pulled from.

Read More

The actual code being used is <?php echo wp_logout_url( $redirect ); ?>. We have tried a simple HTML href but the logout link is dynamic and requires a unique nonce value to validate the command.

Do you have any ideas for how we can have a logout button that uses the actual site address (mywebsite.com) rather than with the added ‘directory’ (mywebsite.com/cs). I have thus far been unable to edit the wp_logout_url to add a / before it. Any ideas?

Example links:
Correct:
http://www.mywebsite.com/backend?action=logout&redirect_to=index.php&_wpnonce=d8eaf8594a

Incorrect, resulting in 404 ERROR:
http://www.mywebsite.com/cs/backend?action=logout&redirect_to=index.php&_wpnonce=d8eaf8594a

Actual code being used (relevant logout code is the #bawlogout# part):

add_filter( 'wp_setup_nav_menu_item', 'bawllm_setup_nav_menu_item' );
function bawllm_setup_nav_menu_item( $item )
{
    global $pagenow;
    if( $pagenow!='nav-menus.php' && !defined('DOING_AJAX') && isset( $item->url ) && strstr( $item->url, '#baw' ) != '' ){
        $item_url = substr( $item->url, 0, strpos( $item->url, '#', 1 ) ) . '#';
        $item_redirect = str_replace( $item_url, '', $item->url );
        switch( $item_url ) {
            case '#bawloginout#' :  
                                    $item_redirect = explode( '|', $item_redirect );
                                    if( count( $item_redirect ) != 2 ) 
                                        $item_redirect[1] = $item_redirect[0];
                                    for( $i = 0; $i <= 1; $i++ ):
                                        if( $item_redirect[$i] == '%actualpage%')
                                            $item_redirect[$i] = $_SERVER['REQUEST_URI'];
                                    endfor;
                                    $item->url = is_user_logged_in() ? wp_logout_url( $item_redirect[1] ) : wp_login_url( $item_redirect[0] );
                                    $item->title = bawllm_loginout_title( $item->title ) ; break;
            case '#bawlogin#' :     $item->url = wp_login_url( $item_redirect ); break;
            case '#bawlogout#' :    $item->url = wp_logout_url( $item_redirect ); break;
            case '#bawregister#' :  if( is_user_logged_in() ) $item->title = '#bawregister#'; else $item->url = site_url( '/wp-login.php?action=register', 'login' ); break;
        }
        $item->url = esc_url( $item->url );
    }
    return $item;
}

Related posts

Leave a Reply

2 comments

  1. I would put the URL into a variable so that you can perform a regular expression on it, for example:

    <?php
    
    $url       = wp_logout_url( $redirect );
    $fixed_url = preg_replace("/stuff_to_find/", "stuff_to_replace", $url);
    
    ?>