Skip to content

Instantly share code, notes, and snippets.

@imath
Last active Jul 27, 2018
Embed
What would you like to do?
Set default activity filter for BuddyPress (legacy or nouveau template pack)
<?php
/**
* Custom functions
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
function set_default_activity_filter() {
$handle = 'bp-nouveau';
$position = 'before';
if ( 'nouveau' !== bp_get_theme_package_id() ) {
$handle = 'bp-jquery-cookie';
$position = 'after';
}
wp_add_inline_script( $handle, sprintf( '
( function( $ ) {
// Replace with the activity type of your choice.
var defaultActivityType = \'new_member\';
if ( \'bp-nouveau\' === \'%s\' ) {
var activityVars = sessionStorage.getItem( \'bp-activity\' ), store;
if ( activityVars ) {
store = JSON.parse( activityVars );
if ( ! store.filter || \'0\' === store.filter ) {
store.filter = defaultActivityType;
sessionStorage.setItem( \'bp-activity\', JSON.stringify( store ) );
}
}
} else if ( \'undefined\' !== typeof $.cookie ) {
if ( \'-1\' === $.cookie( \'bp-activity-filter\' ) || ! $.cookie( \'bp-activity-filter\' ) ) {
$.cookie( \'bp-activity-filter\', defaultActivityType, {
path: \'/\',
secure: ( \'https:\' === window.location.protocol )
} );
}
}
} )( jQuery );
', $handle ), $position );
}
add_action( 'bp_enqueue_scripts', 'set_default_activity_filter', 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment