Created
November 23, 2016 18:53
-
-
Save christianwach/8d87acbbd2f65eea0ac7ea961a2ccaf8 to your computer and use it in GitHub Desktop.
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: BuddyPress Biography Sync | |
Description: Keeps a BuddyPress xProfile field in sync with the WordPress user description field. | |
Author: Christian Wach | |
Version: 0.1 | |
Author URI: http://haystack.co.uk | |
-------------------------------------------------------------------------------- | |
*/ | |
/** | |
* A class that encapsulates sync functionality between a BuddyPress xProfile | |
* "Biography" field and the WordPress "Biography" field. | |
* | |
* @since 0.1 | |
*/ | |
class BuddyPress_Biography_Sync { | |
/** | |
* "Biography" xProfile field ID. | |
* | |
* Set this to the ID of the xProfile field you have created for "Biography". | |
* | |
* @since 0.1 | |
* @access public | |
* @var array $biography_id The ID of the "Biography" field | |
*/ | |
public $biography_id = 3; | |
/** | |
* Constructor. | |
* | |
* @since 0.1 | |
*/ | |
public function __construct() { | |
// register hooks on BuddyPress loaded | |
add_action( 'bp_init', array( $this, 'register_hooks' ) ); | |
} | |
/** | |
* Register hooks on BuddyPress init. | |
* | |
* @since 0.1 | |
*/ | |
public function register_hooks() { | |
// populate xProfile field on user registration and update by admins | |
add_action( 'updated_user_meta', array( $this, 'update_buddypress' ), 30, 4 ); | |
// sync xProfile content to WordPress when Member Profile is saved | |
add_action( 'xprofile_updated_profile', array( $this, 'update_wordpress' ), 9, 3 ); | |
add_action( 'bp_core_signup_user', array( $this, 'update_wordpress' ), 9, 3 ); | |
add_action( 'bp_core_activated_user', array( $this, 'update_wordpress' ), 9, 3 ); | |
} | |
/** | |
* Intercept BuddyPress xProfile data on save and update WordPress user meta. | |
* | |
* @since 0.1 | |
* | |
* @param int $user_id The WordPress user ID | |
* @param array $posted_field_ids The array of submitted xProfile fields | |
* @param bool $errors Any errors | |
*/ | |
public function update_wordpress( $user_id = 0, $posted_field_ids, $errors ) { | |
// make sure we have a user | |
if ( empty( $user_id ) ) { | |
$user_id = bp_loggedin_user_id(); | |
} | |
// bail if we don't receive a user ID | |
if ( empty( $user_id ) ) return; | |
// bail if not the "Biography" field ID | |
if ( ! in_array( $this->biography_id, $posted_field_ids ) ) return; | |
// get the field's content | |
$content = xprofile_get_field_data( $this->biography_id, $user_id ); | |
// prevent recursion | |
remove_action( 'updated_user_meta', array( $this, 'update_buddypress' ), 30 ); | |
// write to user meta | |
update_user_meta( $user_id, 'description', $content ); | |
// re-add hook | |
add_action( 'updated_user_meta', array( $this, 'update_buddypress' ), 30, 4 ); | |
} | |
/** | |
* Intercept WordPress biography data updates and sync to an xProfile field. | |
* | |
* @since 0.1 | |
* | |
* @param int $meta_id The ID of updated metadata entry. | |
* @param int $user_id The WordPress user ID. | |
* @param string $meta_key The Meta key. | |
* @param mixed $meta_value The Meta value. | |
*/ | |
public function update_buddypress( $meta_id, $user_id, $meta_key, $meta_value ) { | |
// bail if not description field | |
if ( $meta_key != 'description' ) return; | |
// save content to "about" xProfile field | |
xprofile_set_field_data( $this->biography_id, $user_id, $meta_value ); | |
} | |
} | |
global $bp_bio_sync; | |
$bp_bio_sync = new BuddyPress_Biography_Sync; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment