WordPress.org

Ready to get started?Download WordPress

Forums

Logout Widget (15 posts)

  1. sblob
    Member
    Posted 1 year ago #

    I have created a custom login/logout widget on the sidebar. However when a logged in user clicks "logout" it gives the following error: WordPress Failure Notice.You are attempting to log out of websitename
    Do you really want to log out?
    The actual code for the logout link I am using is: <a href="http://www.websitename/wp-login.php?action=logout" alt="logout" title="Logout">Logout</a>

    I have also tried using this code:
    <a href="<?php echo wp_logout_url(); ?>" title="Logout">Logout</a>
    But then I get the following error:
    Error 404 - Page not found!
    The page you trying to reach does not exist, or has been moved. Please use the menus or the search box to find what you are looking for.

    The site of concern is http://www.aintreelane.co.za

    Would anyone be able to assist with this?
    Is this a theme problem or something I am doing wrong?
    Thanks in advance!

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    I've implemented a custom login/logout widget in one of my themes. Perhaps you could download it and grab the code that I used?

  3. sblob
    Member
    Posted 1 year ago #

    That would be great! Which theme should I download to find the code?

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Emporium. Have a look at /library/login-widget.php. The script is loaded using:

    // Load login form widget
    function emporium_load_login_widget() {
    	include_once get_template_directory() . '/library/login-widget.php';
    	if( function_exists ( 'wp_login_form' ) ) register_widget( 'Emporium_Login_Widget' );
    }
    add_action( 'widgets_init', 'emporium_load_login_widget' );

    in functions.php

  5. sblob
    Member
    Posted 1 year ago #

    Thank you, I appreciate your help!

    My previous query was dumb as you already linked to emporium.

    I have tried using your code and it gives me the same issue: I am getting a 404 error.
    Unfortunately I don't know enough php to solve why this is happening.

  6. bcworkz
    Member
    Posted 1 year ago #

    The WordPress failure might be because you are not providing a nonce parameter to wp-login.php.

    Not sure about the 404 error. Any chance your theme or a plugin is filtering 'site_url' or something similar? Also, confirm the url you entered in the admin settings is correct. I once had a similar problem and found the wordpress url somehow got set to something like http:\\http:\\www.sample.com

  7. sblob
    Member
    Posted 1 year ago #

    The URL I entered in the admin settings seems fine.
    The code I am currently using is:
    http://www.aintreelane.co.za/wp-login.php?action=logout

    Do you know I link where I can read more about nonce parameters? I'm not sure what parameter parameter to insert?
    Sorry for noob questions!

  8. bcworkz
    Member
    Posted 1 year ago #

    Everyone here was a noob once :)
    The overview is WordPress Nonces
    FYI, the logout action checks the passed nonce with check_admin_referer('log-out'); which means you should do something like

    $logout_url = 'http://www.aintreelane.co.za/wp-login.php?action=logout';
    $logout_url = wp_nonce_url( $logout_url, 'log-out' );

    which should yield something like http://www.aintreelane.co.za/wp-login.php?action=logout&_wpnonce=cfb7bcd268 .

  9. sblob
    Member
    Posted 1 year ago #

    So that code should go into my php somewhere?

  10. truthseeker83
    Member
    Posted 1 year ago #

    <a href="<?php echo wp_logout_url(esc_url( home_url( '/' )); ?>" title="Logout">Logout</a>

    try this it takes you to homepage

  11. sblob
    Member
    Posted 1 year ago #

    Hi Truthseeker, thanks for the help.
    Unfortunately that code gives me a 404 error :(
    Page not found...

    When I use the standard wordpress meta widget the logout link works fine. Is there a way to see what code they use in that widget link?

  12. truthseeker83
    Member
    Posted 1 year ago #

    sorry a bracket was missing

    <a href="<?php echo wp_logout_url(esc_url( home_url('/'))); ?>" title="Logout">Logout</a>

    takes to home page on logout

  13. sblob
    Member
    Posted 1 year ago #

    Still the same issue :(

    Error 404 - Page not found!

    The page you trying to reach does not exist, or has been moved. Please use the menus or the search box to find what you are looking for.

    Is this code you provided the code that the meta widget logout uses? Do you know if there is a way to find this code?

    Really appreciate all the help!

  14. bcworkz
    Member
    Posted 1 year ago #

    So that code should go into my php somewhere?

    Right, the output of wp_nonce_url() should be the href for the link the user clicks to logout.

    The Meta widget source is in wp-includes/default-widgets.php, around line 298. You'll find it simply calls wp_loginout(), which will display the appropriate link depending on the user's status of in or out. If you check it's source, you'll see it uses wp_nonce_url() as well.

  15. sblob
    Member
    Posted 1 year ago #

    That is what I thought. I can't figure out why my custom link won't work but the logout meta widget works.
    Will check the source out!

Topic Closed

This topic has been closed to new replies.

About this Topic