Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Require a login for sitewide activity

  • @wirelab

    Participant

    Hi,

    As above I am trying to require login for sitewide activity. I do not wish to use a plug in so I created a new page template with the code below which works fine for normal pages. However when I set a page as buddypress activity stream its seems to override the page template. Can anyone help please?

    <?php if(is_user_logged_in()):?>
    <?php
    else:
    wp_die("<h2 align='center'>
    To view this page you must first
    log in
    </h2>");
    endif;
    ?>
Viewing 9 replies - 1 through 9 (of 9 total)
  • @wirelab

    Participant

    sorry buddypress 2.1 wordpress 4.0

    @shanebp

    Moderator

    Try this in bp-custom.php

    function wirelab_check_activity_login() { 
      if( ! is_user_logged_in() ) {
        wp_die("To view this page you must first log in.");
      }
    }
    add_action('bp_before_directory_activity', 'wirelab_check_activity_login');

    @hnla

    Participant

    Really! Running a die function midway through rendering a document, in fact what point is there to that extension of PHP die() in rendering html if ones intending on breaking a page beyond redemption. it’s a debugging function you can’t use it in a production site where rendering a page.

    @shanebp

    Moderator

    Thanks for your input, lol.
    Would you find this more acceptable?

    function wirelab_check_activity_login() { 
      if( ! is_user_logged_in() && bp_is_activity() ) {
        wp_redirect( 'url to login page' ); 
        exit;
      }
    }
    add_action('bp', 'wirelab_check_activity_login');

    @hnla

    Participant

    yep think so 😉 OP take note please, not sure where you picked up on the use of die() but you don’t want to be doing that 🙂

    and WP really trying to fathom how ability to render markup in a php function that effectively renders all further action null is at all useful or maybe we’re meant to render a full DOM via it? 🙂

    @wirelab

    Participant

    Hi,

    Sorry i need a bit more help as I failed to get it to work. So I created a bp-custom.php under wp-content/plugins

    and added the following below, however my activity-stream page was still accessible without login

    <?php
    // hacks and mods will go here
    function wirelab_check_activity_login() { 
      if( ! is_user_logged_in() && bp_is_activity() ) {
        wp_redirect( '/wp-login.php' ); 
        exit;
      }
    }
    add_action('bp', 'wirelab_check_activity_login');
    ?>

    @shanebp

    Moderator

    Try this instead:

    function wirelab_check_activity_login() { 
    	if ( bp_is_activity_component() && ! is_user_logged_in() ) {
    		wp_redirect( get_option('siteurl') . '/wp-login.php' );
    		exit;
    	}
    }
    add_action('get_header', 'wirelab_check_activity_login');

    @wirelab

    Participant

    Sorry no luck. My bp-custom.php reads as below

    <?php
    function wirelab_check_activity_login() {
    if ( bp_is_activity_component() && ! is_user_logged_in() ) {
    wp_redirect( get_option('siteurl') . '/wp-login.php' );
    exit;
    }
    }
    add_action('get_header', 'wirelab_check_activity_login');
    ?>

    @wirelab

    Participant

    Update: I added the function to my child theme functions.php and it seems to be working now.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Require a login for sitewide activity’ is closed to new replies.
Skip to toolbar