Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #986037

    ponberries
    Participant

    Does anyone know how to add a logout function to a password protected page? I’m working with a client and they’d like to add a ‘client information’ page which is to be password protected.

    It’s too much content to use the protected content element so I used the password protected option that WordPress provides. The only issue is logging clients out – it’s a security issue to not have an easy way for them to log out.

    I’ve tried a couple of plugins however they don’t fully work. One logs the user out, however it’s not apparent you’ve been logged out until the page is refreshed. Even if a user moves about the site and returns to the client info page, the content is still showing. So until there is a refresh the content is visible.

    I don’t need anything special just a code snippet or something to easily log users out.

    Thanks!

    #986212

    Rahul
    Moderator

    Hi there,

    Thanks for writing in!

    This feature isn’t available yet. However, you can add a logout button when user is logged by using following workaround.

    Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.

    After that, add following code in your child theme’s functions.php file:

    // [protect]: Adding Logout link when Logged in
    // =============================================================================
    
    function x_shortcode_protected2( $atts, $content = null ) {
      extract( shortcode_atts( array(
        'id'    => '',
        'class' => '',
        'style' => ''
      ), $atts, 'protect' ) );
    
      GLOBAL $user_login;
    
      $id    = ( $id    != '' ) ? 'id="' . esc_attr( $id ) . '"' : '';
      $class = ( $class != '' ) ? 'x-protect ' . esc_attr( $class ) : 'x-protect';
      $style = ( $style != '' ) ? 'style="' . $style . '"' : '';
    
      if ( is_user_logged_in() ) {
        $output = do_shortcode( $content . '[gap size="20px"][button href="' . wp_logout_url() . '"]Logout[/button]');
      } else {
        $output = "<div {$id} class=\"{$class}\" {$style}>"
                  . '<form action="' . esc_url( site_url( 'wp-login.php' ) ) . '" method="post" class="mbn">'
                    . '<h6 class="h-protect man">' . esc_html__( 'Restricted Content Login', '__x__' ) . '</h6>'
                    . '<div><label>' . esc_html__( 'Username', '__x__' ) . '</label><input type="text" name="log" id="log" value="' . esc_attr( $user_login ) . '" /></div>'
                    . '<div><label>' . esc_html__( 'Password', '__x__' ) . '</label><input type="password" name="pwd" id="pwd" /></div>'
                    . '<div><input type="submit" name="submit" value="' . esc_html__( 'Login', '__x__' ) . '" class="x-btn x-btn-protect" /></div>'
                    . '<input type="hidden" name="redirect_to" value="' . esc_url( get_permalink() ) . '">'
                  . '</form>'
                . '</div>';
      }
    
      return $output;
    }
    
    add_filter('init', 'update_x_shortcode_protected');
    
    function update_x_shortcode_protected() {
      remove_shortcode( 'protect' );
    
      add_shortcode( 'protect', 'x_shortcode_protected2' );
    }

    Hope this helps. 🙂

    Thank you.