0

I have a plugin and want to add feature to make first_name and last_name required fields.

When the user goes to their profile page, then they must add a first_name and last_name.

There are also some custom WooCommerce fields that I want to make required as well.

Is there a pre-save-profile hook/filter?

Thanks

3

You could check the $_POST variable when updating the user profile page.

I would hook into user_profile_update_errors and do something along those lines. This filter will not save to DB if errors are found.

add_filter('user_profile_update_errors', 'wpse_236014_check_fields', 10, 3);
function wpse_236014_check_fields($errors, $update, $user) {

  // Use the $_POST variable to check required fields

  if( empty($_POST['first_name']) )
    // add an error message to the WP_Errors object 
    $errors->add( 'first_name_required',__('First name is required, please add one before saving.') );

  if( empty($_POST['last_name']) )
    // add an error message to the WP_Errors object 
    $errors->add( 'last_name_required',__('Last name is required, please add one before saving.') );

  // Add as many checks as you have required fields here

  if( empty( $errors->errors ) ){

    // Save your custom fields here if no errors are found
    // Just skip this if you don't need to do extra work.
    // Fields will save if no errors are found

  }


}

EXTRA

There is also 2 hooks that you could use when saving custom fields added to the profile page. I'll refer you to the codex if you want more specifications as I'm pretty sure you won't need those for your specific use case as I believe WooCommerce custom fields should appear in the $_POST variable as well.

  • I had seen the two _update filters but not the update_errors one. That was what I needed (and yes had to use $_POST variable to see what was submitted). Accepted answer! – jsherk Aug 16 '16 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.