daily pastebin goal
71%
SHARE
TWEET

Untitled

a guest Dec 10th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class BP_Other_User_As_Profile_Field {
  2.  
  3.     /**
  4.      * Field id, replace with actual field id.
  5.      *
  6.      * @var string
  7.      */
  8.     private $field_id = 25; // Chang it with actual field id.
  9.  
  10.     /**
  11.      * BP_Other_User_As_Profile_Field constructor.
  12.      */
  13.     public function __construct() {
  14.         add_action( 'bp_signup_validate', array( $this, 'validate' ) );
  15.         add_filter( 'bp_get_the_profile_field_value', array( $this, 'filter' ) );
  16.     }
  17.  
  18.     /**
  19.      * Validate fields.
  20.      */
  21.     public function validate() {
  22.         if ( ! bp_is_active( 'xprofile' ) ) {
  23.             return;
  24.         }
  25.  
  26.         $profile_field_ids = isset( $_POST['signup_profile_field_ids'] ) ? explode( ',', $_POST['signup_profile_field_ids'] ) : array();
  27.  
  28.         if ( ! in_array( $this->field_id, $profile_field_ids ) ) {
  29.             return;// no need to validate, the field is not available on signup page.
  30.         }
  31.  
  32.         if ( empty( $_POST[ 'field_' . $this->field_id ] ) ) {
  33.             return;// if it is required, bp will take care.
  34.         }
  35.  
  36.         $email = trim( $_POST[ 'field_' . $this->field_id ] );
  37.         $user  = get_user_by( 'email', $email );
  38.         if ( ! $user ) {
  39.             buddypress()->signup->errors[ 'field_' . $this->field_id ] = __( 'Please enter a valid email' );
  40.         }
  41.     }
  42.  
  43.     /**
  44.      * Filter value to link to user profile.
  45.      *
  46.      * @param mixed $value value.
  47.      *
  48.      * @return mixed|string
  49.      */
  50.     public function filter( $value ) {
  51.         global $field;
  52.  
  53.         if ( $this->field_id != $field->id || empty( $value ) ) {
  54.             return $value;
  55.         }
  56.  
  57.  
  58.         $user = get_user_by( 'email', $value );
  59.         if ( ! $user ) {
  60.             return 'N/A'; //invalid user.
  61.         }
  62.  
  63.         return bp_core_get_userlink( $user->ID );
  64.     }
  65. }
  66.  
  67. new BP_Other_User_As_Profile_Field();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top