Skip to content

Instantly share code, notes, and snippets.

@slaFFik
Forked from technosailor/ab-bp-retroactiveusers.php
Last active November 16, 2021 22:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slaFFik/46ae6c250f6e4376c4b21cc0ef6b57ab to your computer and use it in GitHub Desktop.
Save slaFFik/46ae6c250f6e4376c4b21cc0ef6b57ab to your computer and use it in GitHub Desktop.
Populate WordPress User Meta so that even users who have never logged in will display on the Members BP page
<?php
/*
Plugin Name: Retroactive BP User Acticity
Plugin URI: https://gist.github.com/3953927
Description: Makes all BuddyPress users visible immediately on user creation and retroactively adjust users to allow for their display before logging in.
Author: Aaron Brazell
Version: 1.0
Author URI: http://technosailor.com
License: MIT
License URI: http://opensource.org/licenses/MIT
*/
/**
* A BuddyPress class for user creation simplicity
*
* @author Aaron Brazell <aaron@technosailor.com>
* @package ab-bp-prepopulate-users
*/
class BP_Prepopulate_Users {
// MySQL default limit is 1000, but you might have more users, so modify this value accordingly.
const USERS_NUMBER = 1000;
/**
* PHP constructor method. Requires PHP 5.2+
*
* @author Aaron Brazell <aaron@technosailor.com>
* @package ab-bp-prepopulate-users
*/
public function __construct() {
$this->hooks();
}
/**
* Method to tie methods in this class into the WordPress hook system
*
* @author Aaron Brazell <aaron@technosailor.com>
* @package ab-bp-prepopulate-users
* @return void
*/
public function hooks() {
// Fix existing users. Will only ever be run once
add_action( 'wp_loaded', array( $this, 'retroactive_user_fix' ) );
// New users get the last_active meta
add_action( 'user_register', array( $this, 'user_active_on_create' ) );
}
/**
* A method for retroactively loading existing users with the last_active meta_key. BuddyPress will not show
* users who have never logged in so this is a quick fix to get around that problem. Method stores the abbp_retroactive
* option in the WordPress options table after it is run once on page load. After that, the method short circuits
* if the option exists, preventing unnecessary database queries in the future.
*
* @author Aaron Brazell <aaron@technosailor.com>
* @package ab-bp-prepopulate-users
* @return void
*/
public function retroactive_user_fix() {
if( get_option( 'abbp_retroactive' ) ) {
return;
}
$users = get_users( array(
'fields' => 'ID',
'number' => BP_Prepopulate_Users::USERS_NUMBER
) );
foreach( $users as $user_id ) {
// Check to see if the user has activity
if ( bp_get_user_last_activity( $user_id ) ) {
continue;
}
// Inactive so lets activate
bp_update_user_last_activity( $user_id, bp_core_current_time() );
}
// Clean transient cache
delete_transient( 'bp_active_member_count' );
// Only do this once so not to tax the database unnecessarily
add_option( 'abbp_retroactive', true );
}
/**
* A method for pre-populating the last_active meta_key in the usermeta table. BuddyPress only displays users with
* this key so we force the user to have that key to get around the limitation. Hooks on the user_register hook.
*
* @author Aaron Brazell <aaron@technosailor.com>
* @package ab-bp-prepopulate-users
* @return void
*/
public function user_active_on_create( $user_id ) {
bp_update_user_last_activity( $user_id, bp_core_current_time() );
// Clean transient cache, just in case
delete_transient( 'bp_active_member_count' );
}
}
new BP_Prepopulate_Users;
@renatonascalves
Copy link

Interesting!

@ilonabudapesti
Copy link

This is great, thanks, exactly what we needed. How would we include or require this? Or is require_once enough? Thanks!

Copy link

ghost commented May 3, 2019

Superb, thanks so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment