Skip to content

Instantly share code, notes, and snippets.

@technosailor
Created October 25, 2012 16:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save technosailor/3953927 to your computer and use it in GitHub Desktop.
Save technosailor/3953927 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_Prepoulate_Users {
/**
* 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( 'init', 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 false;
$users = get_users();
foreach( $users as $user )
{
// Check to see if the user is active
if( get_user_meta( $user->ID, 'last_activity', true ) )
continue;
// Inactive so lets activate
$timestamp = date( 'Y-m-d G:i:s' );
add_user_meta( $user->ID, 'last_activity', $timestamp );
}
// 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 )
{
$timestamp = date( 'Y-m-d G:i:s' );
add_user_meta( $user_id, 'last_activity', $timestamp );
}
}
$abbppu = new BP_Prepoulate_Users;
@tivnet
Copy link

tivnet commented Mar 17, 2017

Typo class BP_Prepoulate_Users missing p

@technosailor Thanks!

@slaFFik
Copy link

slaFFik commented Mar 17, 2017

A try to "modernize" this code for the latest BuddyPress: https://gist.github.com/slaFFik/46ae6c250f6e4376c4b21cc0ef6b57ab

@ilonabudapesti
Copy link

ilonabudapesti commented Aug 9, 2018

This is perfect, thanks, just what we need. How would we include or require this? Or is require_once enough? Thanks! I will use the "modernized" version above, just wanted to thank and acknowledge Aaron's work.

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