Require login to access wordpress site

Asked

Viewed 545 times

0

I have to make a portal, but it can only be accessed by authorized people, how do I do this with wordpress? I am well beginner and do not know how to proceed in this case

1 answer

1

Direct option inside the Theme.

In the functions.php of your theme put this code. Project link on Github: https://github.com/kevinvess/wp-force-login

  $url  = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
  $url .= '://' . $_SERVER['SERVER_NAME'];
  $url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT'];
  $url .= $_SERVER['REQUEST_URI'];
  return $url;
}
function v_forcelogin() {
  if( !is_user_logged_in() ) {
    $url = v_getUrl();
    $whitelist = apply_filters('v_forcelogin_whitelist', array());
    $redirect_url = apply_filters('v_forcelogin_redirect', $url);
    if( preg_replace('/\?.*/', '', $url) != preg_replace('/\?.*/', '', wp_login_url()) && !in_array($url, $whitelist) ) {
      wp_safe_redirect( wp_login_url( $redirect_url ), 302 ); exit();
    }
  }
}
add_action('init', 'v_forcelogin');

If you want to use the snippet plugin above I suggest using this plugin https://wordpress.org/plugins/wp-force-login/ inserir a descrição da imagem aqui


Another plugin option is the Restrict Content (There is paid version has to see if the free meets you): https://wordpress.org/plugins/restrict-content/

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.