Created
October 25, 2012 16:41
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
* @package ab-bp-prepopulate-users | |
*/ | |
class BP_Prepoulate_Users { | |
/** | |
* PHP constructor method. Requires PHP 5.2+ | |
* | |
* @author Aaron Brazell <[email protected]> | |
* @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 <[email protected]> | |
* @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 <[email protected]> | |
* @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 <[email protected]> | |
* @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; |
A try to "modernize" this code for the latest BuddyPress: https://gist.github.com/slaFFik/46ae6c250f6e4376c4b21cc0ef6b57ab
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
Typo
class BP_Prepoulate_Users
missingp
@technosailor Thanks!