BuddyDev

Search

[Resolved] insert a value into my new xprofile numeric field

  • Participant
    Level: Enlightened
    Posts: 126
    Hervé on #14308

    hello,

    I found a way to insert a value into my new xprofile numeric field of an existing database.
    https://buddypress.org/support/topic/sql-query-program-or-plugin-to-insert-a-value-in-a-field-for-all-members/

    $users = get_users( [ 'fields' => 'ids' ] );
    global $wpdb;
    /** @var \WP_User $user */
    foreach ( $users as $user_id ) {        
        $results = $wpdb->insert(
            'bp_xprofile_data',
            [
                'field_id'     => 794,
                'user_id'      => $user_id,
                'value'        => 4,
                'last_updated' => new \DateTime()
            ],
            [ '%d', '%d', '%d', '%s' ]
        );
    }

    How to make the members who register automatically have a default value in this field by default (am I obliged to use a field choice ?

    Regards

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #14316

    Hi Herve,
    You may use the following code

    
    
    /**
     * Set an xprofile field value on account activate.
     *
     * @param int $user_id user id.
     */
    function buddydev_insert_preset_field_value( $user_id ) {
    	$field_id = 794;
    	xprofile_set_field_data( $field_id, $user_id, 4 );
    }
    add_action( 'bp_core_activated_user', 'buddydev_insert_preset_field_value' );
    
    

    That will insert/update the field value when user account is activated.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 126
    Hervé on #14392

    Hi Brajesh

    Thank you it works.
    Great 🙂
    If I understand correctly, I can use add_action (‘bp_core_activated_user’, ..) to do any type of action when registering a new member

    and there is another hook for editing data change a profile:
    “xprofile_data_before_save”

    BuddyPress has a lot of HOOKS
    Regards

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #14399

    Hi Herve,
    You are welcome.

    yes, The above hook fires when a user activates their account.

    For editing the profile, I will suggest sticking to ‘xprofile_updated_profile’ hook as the ‘xprofile_data_before_save’ fires each time an individual xprofile field is updated.

    Best Regards
    Brajesh

The topic ‘ [Resolved] insert a value into my new xprofile numeric field’ is closed to new replies.

This topic is: resolved