Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use a text field as a way to link to other users profile on BuddyPress
class BP_Other_User_As_Profile_Field {
/**
* Field id, replace with actual field id.
*
* @var string
*/
private $field_id = 25; // Chang it with actual field id.
/**
* BP_Other_User_As_Profile_Field constructor.
*/
public function __construct() {
add_action( 'bp_signup_validate', array( $this, 'validate' ) );
add_filter( 'bp_get_the_profile_field_value', array( $this, 'filter' ) );
}
/**
* Validate fields.
*/
public function validate() {
if ( ! bp_is_active( 'xprofile' ) ) {
return;
}
$profile_field_ids = isset( $_POST['signup_profile_field_ids'] ) ? explode( ',', $_POST['signup_profile_field_ids'] ) : array();
if ( ! in_array( $this->field_id, $profile_field_ids ) ) {
return;// no need to validate, the field is not available on signup page.
}
if ( empty( $_POST[ 'field_' . $this->field_id ] ) ) {
return;// if it is required, bp will take care.
}
$email = trim( $_POST[ 'field_' . $this->field_id ] );
$user = get_user_by( 'email', $email );
if ( ! $user ) {
buddypress()->signup->errors[ 'field_' . $this->field_id ] = __( 'Please enter a valid email' );
}
}
/**
* Filter value to link to user profile.
*
* @param mixed $value value.
*
* @return mixed|string
*/
public function filter( $value ) {
global $field;
if ( $this->field_id != $field->id || empty( $value ) ) {
return $value;
}
$user = get_user_by( 'email', $value );
if ( ! $user ) {
return 'N/A'; //invalid user.
}
return bp_core_get_userlink( $user->ID );
}
}
new BP_Other_User_As_Profile_Field();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.