Tagged: buddypress, xprofile field
Hello frends!
Is there a way to update the user profile field bypassing BuddyPress?
I would be glad for any help, thanks
Here is an example of the form:
<form id=”myform” method=’post’ action=” required>
<select id=”mylogins” class=’livesearch’ required>
<option value=””>Please select login</option>
<?php if ( bp_group_has_members( bp_ajax_querystring( ‘group_members’ ) ) ) : ?>
<?php while ( bp_group_members() ) : bp_group_the_member(); ?><option value='<?php echo bp_get_member_user_id(); ?>’><?php bp_member_user_nicename(); ?> <?php bp_group_member_name(); ?></option>
<?php endwhile; ?>
<?php endif; ?>
</select>
<?php
$field_id = 49;
$user_id = $_POST[‘mylogins’];
?><input type=”text” placeholder=”Enter value” required>
<input type=”submit” id=”myform” value=”Save”>
</form>
Hello Vlad,
Thank you for posting here. Please look at the following function you can use to save the xprofile field value.
https://github.com/buddypress/buddypress/blob/master/src/bp-xprofile/bp-xprofile-functions.php#L432
Please check
Regards
RaviHi Vlad,
You will need to write a form handler. Something that will handle the submission of your form. in order to do that, you need to identify your form.I had a look at your form code above and I am not sure what you want to achieve with it. Can you please help us understand what you are trying to achieve with it? Ravi or I will be able to assist you better then.
Regards
BrajeshHi Vlad,
The code to update will have 2 parts. A form and a handler. The form is placed somewhere in your group screen and the handler code should be in your theme’s functions.php.
Here is your form updated slightly.
<form id="myform" method='post' action=""> <select id="mylogins" name="mylogins" class='livesearch' required> <option value="">Please select login</option> <?php if ( bp_group_has_members( bp_ajax_querystring( 'group_members' ) ) ) : ?> <?php while ( bp_group_members() ) : bp_group_the_member(); ?> <option value='<?php echo bp_get_member_user_id(); ?>'><?php bp_member_user_nicename(); ?><?php bp_group_member_name(); ?></option> <?php endwhile; ?> <?php endif; ?> <?php $group_id = bp_get_group_id( groups_get_current_group() ); ?> </select> <input type="hidden" name="group-meta-form-group-id" value="<?php echo esc_attr( $group_id ); ?>"> <input type="text" placeholder="Enter value" name="my-field-name" required> <?php wp_nonce_field( '_group_meta_update_' . $group_id, '_group_meta_update_nonce' ); ?> <input type="submit" id="myform" value="Save"> </form>
and here is the handler code
add_action( 'bp_actions', function () { // make sure it is our form submission. if ( ! isset( $_POST['group-meta-form-group-id'] ) ) { return; } $field = 'My Field Name'; // Please Update it with Your Xprofile field id or name. // Only logged users can access it. if ( ! is_user_logged_in() ) { return; } $grooup_id = absint( $_POST['group-meta-form-group-id'] ); if ( ! $grooup_id || ! wp_verify_nonce( $_POST['_group_meta_update_nonce'], '_group_meta_update_' . $grooup_id ) ) { return; } $selected_user_id = isset( $_POST['mylogins'] ) ? absint( wp_unslash( $_POST['mylogins'] ) ) : 0; if ( ! $selected_user_id || ! get_user_by( 'id', $selected_user_id ) ) { return; } $logged_user_id = get_current_user_id(); if ( is_super_admin( $logged_user_id ) || groups_is_user_admin( $logged_user_id, $grooup_id ) || groups_is_user_mod( $logged_user_id, $grooup_id ) ) { // update profile. $value = isset( $_POST['my-field-name'] ) ? sanitize_text_field( wp_unslash( $_POST['my-field-name'] ) ) : ''; if ( empty( $value ) ) { xprofile_delete_field_data( $field, $selected_user_id ); } else { xprofile_set_field_data( $field, $selected_user_id, $value ); } bp_core_add_message( __( 'Updated successfully.', 'some -text-domain' ), 'success' ); } } );
I haven’t tested it but I hope they will work fine.
Please give it a try and adapt as you need it.
Regards
BrajeshHi Brajesh!
You are my super star !!!
Helped again!
Everything works as it should.Changed a little.
Field name – group slug$field = ‘My Field Name’; // Please Update it with Your Xprofile field id or name.
$field = bp_get_group_slug( groups_get_current_group() );Thank you very much!!!
Hi Vlad,
Thank you for the kind words.I am glad it worked 🙂
PS:- You are using the Group slug as Profile field name. I assume you have create an xprofile field for each of group.
Regards
Brajesh
You must be logged in to reply to this topic.