User Profile Page – Edit Profile Update Message

Solved7.65K viewsIssues
0

Hello Rahul,

Thanks for the recent update in Askbug where you fixed the edit user profile page issue. Just a small addition to that, the page doesn’t display any message on updating the user profile. For now you are just printing “true”, which is the ajax call response in the JS Console. Is it possible to process the response of the ajax call and display a snackbar message?

selected answer
0

Hello Lisa,

Thanks for posting your solution here. But achieving this is much simpler then you think. you just have to replace wp_die() with this:

if ( ! is_wp_error( $user_id ) ) {
  ap_send_json( array(
    'success' => true,
    'snackbar' => [ 'message' => __( 'Profile Updated Successfully', 'askbug' ) ],
  ) );
}

ap_send_json( array(
  'success' => false,
  'snackbar' => [ 'message' => __( 'Failed to update profile', 'askbug' ) ],
) );

No JS code required. I have added this to theme.
Cheers.

commented on answer

Awesome. But, don’t you need the AnsPress.trigger(‘snackbar’, parsedData) function in JS to display the message? Right now, it is just printing the response to the JS Console.

Also, Is the update ready to download?

Not yet. Will publish new release after few more bug fixes.

I just updated the code with the above one and removed the extra JS I had added. It still works.. !! Great.

Thanks.

0

Hello Rahul,

I fixed this on my own. I’m not sure if it follows your dev guidelines but please see if you can add the below modifications to the next update.

#1 I updated the ab_update_user_profile() function in WP_THEMES_FOLDER/askbug/includes/hooks.php between line 182 to 209

The changes are in line 23 and 26 in the code below:

function ab_update_user_profile() {
	if ( ! is_user_logged_in() || ! wp_verify_nonce( $_POST['__nonce'], 'update_profile' ) ) {
		wp_die( 'false' );
	}

	$allowed = [ 'first_name', 'last_name', 'display_name', 'nickname', 'description', 'user_url' ];

	$form = [ 'ID' => get_current_user_id() ];
	foreach ( $allowed as $key ) {
		if ( isset( $_POST[ $key ] ) ) {

			if ( 'user_url' === $key ) {
				$form[ $key ] = esc_url( $_POST[ $key ] );
			} else {
				$form[ $key ] = ap_sanitize_unslash( $_POST[ $key ] );
			}
		}
	}

	$user_id = wp_update_user( $form );

	if ( ! is_wp_error( $user_id ) ) {
		wp_die( '<div id="ap-response">{"success":true,"snackbar":{"message":"Profile Updated Successfully"},"ap_response":true,"is_ap_ajax":true}</div>' );
	}

	wp_die( '<div id="ap-response">{"success":false,"snackbar":{"message":"Unable to update profile"},"ap_response":true,"is_ap_ajax":true}</div>' );
}
add_action( 'ap_ajax_ab_update_user_profile', 'ab_update_user_profile' );

#2 I updated the submit() function called on the with id ab_edit_profile in WP_THEMES_FOLDER/askbug/includes/js/askbug.js between line 80 and 88

The changes are between line 5 to 9 in the code below:

 $('#ab_edit_profile').on('submit', function(){
            AnsPress.ajax({
                data: $(this).serialize(),
                success: function(data){
                    var parsedData = AnsPress.ajaxResponse(data);
                    console.log(parsedData);
                    if(parsedData.snackbar){
                        AnsPress.trigger('snackbar', parsedData)
                    }
                }
            });
            return false;
        });

This works well for me. Attached image below showing success message ?

I hope the above changes are incorporated in the future release of Askbug.

Thanks,
Lisa

edited answer