WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Forcing Logout from the Admin Panel (4 posts)

  1. goxu
    Member
    Posted 7 years ago #

    Hi all,

    I've been trying to force anybody in the admin panel to logout. :) Actually I want to use this for a different purpose(security) but for the basic, I'd need to get through this step.

    I expected the following code does the job but it doesn't.

    function force_logout () {
    wp_clearcookie();
    do_action('wp_logout');
    nocache_headers();
    $redirect_to = 'wp-login.php';
    wp_redirect($redirect_to);
    exit();
    }
    add_action ('admin_head', 'force_logout');

    Does anybody know the solution for this?

  2. goxu
    Member
    Posted 7 years ago #

    Ok,

    If I embed the following code at the very beggining of the wp-admin/admin-header.php it works.

    header( 'Location: ' . get_settings('siteurl') );
    exit();

    However, if I do this as a plugin,

    /*
    Plugin Name: Go Home
    Plugin URI:
    Description: forces anybody to go home
    Version: 0.1beta
    Author: Goxu
    Author URI:
    */

    function force_gohome() {
    header( 'Location: ' . get_settings('siteurl') );
    exit();
    }

    add_action ('admin_header', 'force_gohome' ,1);`

    The priority is set to 1, so I assume that the funciton will be loaded prior to anyother funciotns. Am I correct?

    If so, why doesn't it work? Any information will be greatly appreciated.

  3. goxu
    Member
    Posted 7 years ago #

    I haven't been able to figure out yet.

    There was a minor mistake in the last line. the 'admin_header' had to be 'admin_head'.

    Still, I keep getting the following error in the admin panel after enabling this plugin. Is it only me getting this or do you also get this error too?

    Warning: Cannot modify header information - headers already sent by (output started at /home/wordpress/wp-admin/admin-header.php:16) in /home/wordpress/wp-content/plugins/force_gohome.php on line 12

    <?
    /*
    Plugin Name: Go Home
    Plugin URI: http://example.com
    Description: forces anybody to go home
    Version: 0.1beta
    Author: Goxu
    Author URI: http://example.com
    */
    function force_gohome(){
    header('Location: ' . get_settings('siteurl'));
    exit();
    }
    add_action('admin_head', 'force_gohome', 1);
    ?>

  4. goxu
    Member
    Posted 7 years ago #

    The 'init' hook did the job.

Topic Closed

This topic has been closed to new replies.

About this Topic