BuddyDev

Search

[Resolved] xprofile verify if custom field data exist on registration

  • Participant
    Level: Initiated
    Posts: 3
    leile on #21047

    Hi!

    I am using BuddyPress with “SuitePlugins – Advanced XProfile Fields for BuddyPress”, and I created some custom fields. On the registration page I want to check if ex. “idofcertificate” custom field data exist in the database. If exist then throw an error, to only one account to be created with this “idofcertificate”. There is any ideea how to do this?

    Thank you very much!

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #21073

    Hi,
    Welcome to BuddyDev and thank you for asking the question.

    You can do it on bp_signup_validate action. I am showing it with a example. You will need to check for duplicate on your own as I am not sure how are you storing or from which table you want to check for the data(Is it the profile data table?)

    You will also need to know the field id to find the submitted content

    
    /**
     * Validation error.
     */
    function budydev_custom_validate_signup() {
    	$field_id = 32;// please change with your field id.
    	$field    = isset( $_POST[ 'field_' . $field_id ] ) ? trim( $_POST[ 'field_' . $field_id ] ) : '';
    	// validate your field
    	// from database
    	// and if there is any error,
    	// add the error to BuddyPress as
    	buddypress()->signup->errors[ 'field_' . $field_id ] = "Your error message";
    }
    
    add_action( 'bp_signup_validate', 'budydev_custom_validate_signup' );
    
    

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 3
    leile on #21091

    Hi!

    Thank you very much your help! Yes, I have to check if exist in “bp_xprofile_data” table the data. There are functions what can I use for that? Or it is better if I make a query and check?

    Thank you anyway, it was very helpful!

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #21103

    Hi Leile,

    I don’t see any function that does it. It will be faster and simpler to write a query. If you want, I can do that for you.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 3
    leile on #21107

    Hi!

    Thank you very much, I created the query, then the complete code:

     
    function budydev_custom_validate_signup() {
        global $wpdb;
    	$field_id = 13;
    	$field    = isset( $_POST[ 'field_' . $field_id ] ) ? trim( $_POST[ 'field_' . $field_id ] ) : '';
    
        $qry = "SELECT value FROM wp_bp_xprofile_data WHERE value = " . $field;
        $result = $wpdb->get_results( $qry );
        
        if(!empty($result)) {
            buddypress()->signup->errors[ 'field_' . $field_id ] = 'error message';
        }
    
    } 
  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #21109

    Hi,
    That will work.
    May I suggest using the following

    
    
    $table  = buddypress()->profile->table_name_data;
    $qry    = $wpdb->prepare( "SELECT value FROM {$table} WHERE field_id = %d AND value = %s", $field_id, $field );
    $result = $wpdb->get_var( $qry );
    
    

    instead of

    
    $qry = "SELECT value FROM wp_bp_xprofile_data WHERE value = " . $field;
        $result = $wpdb->get_results( $qry );
    

    It will work even when the prefix for database is different than wp_ and sanitizing the query is important to avoid any issue. Also, It limits the search for the value ot specific field id.

    Bets Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 3
    leile on #21114

    Hi!

    Yes, I changed it, it is more correct and safe code, thank you very much!!!

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #21116

    You are welcome. I am glad it is juseful.

    Bet Regards
    Brajesh

The topic ‘ [Resolved] xprofile verify if custom field data exist on registration’ is closed to new replies.

This topic is: resolved