Support » Plugin: Force Login » Not working for buddypress site

  • Resolved uzosky

    (@uzosky)


    Hello, this is a nice plugin that works well with post, pages and bbpress. Unfortunately its not working with buddypress pages. Its also not working with my homepage, I guess this is because it is set to the buddypress group directory page. What I want is for the login and registration pages to be the only pages accessible to loggedout/unregistered users. Please what is the solution?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Kevin Vess

    (@kevinvess)

    Hi– thanks for using Force Login!

    The only pages accessible to visitors, out-of-the-box, is the default WordPress login/lost-password/register screens.

    If you need to add exceptions to allow visitors to view other pages, try using the bypass filter as described in the FAQ.

    What I want is for the login and registration pages to be the only pages accessible to loggedout/unregistered users.

    You probably need to use BuddyPress Conditional Tags to identify what BuddyPress pages you want Force Login to allow visitors.

    /**
     * Bypass Force Login to allow for exceptions.
     *
     * @return bool Whether to disable Force Login. Default false.
     */
    function my_forcelogin_bypass( $bypass ) {
      if ( function_exists('bp_is_register_page') ) {
        if ( bp_is_register_page() || bp_is_activation_page() ) {
          $bypass = true;
        }
      }
      return $bypass;
    }
    add_filter('v_forcelogin_bypass', 'my_forcelogin_bypass', 10, 1);
    uzosky

    (@uzosky)

    Thank you for your response. I tested the plugin with the WP Twenty Seventeen theme and it worked perfectly. Please, this may be asking too much, but if you have any possible solution to make this plugin work with a custom buddypress theme please share.

    Plugin Author Kevin Vess

    (@kevinvess)

    Great! I’m glad you figured out the cause of your issue. Unfortunately, I won’t be able to help troubleshoot your custom buddypress theme.

    Thanks for using Force Login!

    uzosky

    (@uzosky)

    Hi Kevin, I appreciate your hard work, please consider your effort to be about making this nice plugin work with diverse wordpress custom themes. WP Debug is enabled and its not displaying conflicting errors caused by my custom buddypress theme.

    I disabled the plugin, then paste this function from an older(2014) version of wp-forced-login plugin to my function.php:

    
    function v_getUrl() {
      $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');
    

    This code solves the issue of redirects all pages to the login page. But the whitelist function does not exclude the register/activation pages.

    • This reply was modified 2 years ago by uzosky.
    Plugin Author Kevin Vess

    (@kevinvess)

    For what it’s worth– I suspect the reason the older Force Login code “works” better for your install is because it’s being executed at an earlier hook than the current version.

    add_action('init', 'v_forcelogin');

    The newer versions of Force Login executes at the template_redirect hook – which is a more appropriate time to run and was a change specifically to allow the plugin to be more compatible with a diverse variety of other plugins and themes.

    I tested the plugin with the WP Twenty Seventeen theme and it worked perfectly.

    You said yourself it works “perfectly” with a default Twenty Seventeen theme. Something about your custom theme is causing Force Login to not work as intended.

    WP Debug is enabled and its not displaying conflicting errors caused by my custom buddypress theme.

    Just because WP_DEBUG is not showing any PHP errors, does not mean there aren’t any conflicts. Both your theme and Force Login are running without errors, but something in your theme is executing before and/or overriding Force Login.

    Good luck!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Not working for buddypress site’ is closed to new replies.