How to show login link for non-logged in users instead Buddypress Activity Loop?
Is there an easy way to do this in theme functions.php file?
I know how to add notice before loop with: action_activity_loop_start but how to hide the whole loop?
How to show login link for non-logged in users instead Buddypress Activity Loop?
Is there an easy way to do this in theme functions.php file?
I know how to add notice before loop with: action_activity_loop_start but how to hide the whole loop?
You can use something like this:
add_filter( 'bp_get_template_part', static function( $templates, $slug, $name ) {
if ( $slug === 'activity/index' && ! is_user_logged_in() ) {
$templates = [];
}
return $templates;
}, 10, 3 );
This snippet will completely hide the default activity loop, with all filters, forms, etc, rendering an empty page instead of all BuddyPress Activity.
This may not be a desirable way.
In the $templates
variable you can also define your own path to a custom template (like activity/logged-out.php
, so it will look like this:
$templates = [ 'activity/logged-out.php' ];
You will need to create a wp-content/themes/theme-name/buddypress/activity/logged-out.php
file with something like this:
<?php do_action( 'bp_before_directory_activity' ); ?>
<div id="buddypress">
<?php do_action( 'bp_before_directory_activity_content' ); ?>
<div id="template-notices" role="alert" aria-atomic="true">
<?php do_action( 'template_notices' ); ?>
</div>
<?php do_action( 'bp_before_directory_activity_list' ); ?>
<div class="activity" aria-live="polite" aria-atomic="true" aria-relevant="all">
CUSTOM TEXT FOR LOGGED OUT USERS.
</div><!-- .activity -->
</div>
You can check activity/index.php
file to decide what you need and what you don't, but I think this is a good idea to remove the majority of content and hooks.
If you use the BuddyPress Legacy Template Pack (check in BuddyPress settings) you can copy /wp-content/plugins/buddypress/bp-templates/bp-legacy/buddypress/activity/index.php
file into your theme: wp-content/themes/theme-name/buddypress/activity/index.php
and do the modification where needed. Check the line around 235 which has this: <?php bp_get_template_part( 'activity/activity-loop' ); ?>
.
For BuddyPress Nouveau Template Pack everything is basically the same, but you will need to deal with wp-content/plugins/buddypress/bp-templates/bp-nouveau/buddypress/activity/index.php
file. You will need to wrap its div#activity-stream
into is_user_logged_in()
check before display the whole div and its content, something like this:
<?php if ( is_user_logged_in() ) : ?>
<div id="activity-stream" class="activity" data-bp-list="activity">
<div id="bp-ajax-loader"><?php bp_nouveau_user_feedback( 'directory-activity-loading' ); ?></div>
</div><!-- .activity -->
<?php endif; ?>
This way you will basically override the default BuddyPress template file with your own. Please remember about the ajax functionality of the page (filtering etc).