Add Validation to Custom User Meta on Profile Page of WordPress Admin

I want to extend the existing user roles and capabilities of wordpress. I added custom roles and capabilities. I have also added custom user meta (dob, gender, status, etc…)

Everything is good, except, I want that if a user or the admin tries to update the custom user meta, it should be validated via php, I can do it via JS but I want to add validation to PHP as well.

Read More

personal_options_update and edit_user_profile_update action hooks,
using this, I can successfully update the values of the custom user meta, but cannot validate. (I want to display validation errors, just like wordpress does normally).

so I tried this technique from this link:
https://wordpress.stackexchange.com/questions/76419/validating-a-new-user-registration-field

and used the user_profile_update_errors action hook.

It does most of the job, I can add validation and add it to wordpress error notification. and if there are errors prevent update of custom user meta.

however I noticed is that even if the new data is valid and no validation error occured, it doesn’t update the custom user meta.

below is a sample of my codes:

call back to the user_profile_update_errors action hook, I intentionally did not check if the $errors object has errors to illustrate the somehow the update_user_meta does not get triggered. also this is a function of a class

public function updateCustomUserMeta($errors,$update,$user){

    // Check if user has authority to change this
    if(!current_user_can('read',$user->ID)){
        $errors->add("permission_denied","You do not have permission to update this page");
    }

    // Var declaration and default values
    $mn = "";

    // Validation
    if(isset($_POST["alumni-middlename"])){
        $mn = trim($_POST["alumni-middlename"]);

        if(empty($mn)){
            $errors->add("middlename_empty","Middle Name Empty");
        }
    }else{
        $errors->add("middlename_not_passed","Middle Name Field Not Passed");
    }

    update_user_meta($user->ID,"Middle Name",$_POST["alumni-middlename"]);

}// end updateCustomUserMeta

and this is my plugin in bootstrap code:

add_action( 'user_profile_update_errors',array(AlumniUserRolesAndCapabilities::getInstance(),"validateCustomUserMeta"));

To sum up, I want to be able to validate the custom user meta via php and prevent updating of values if there are errors and display it appropriately normally just like what wordpress does. Also if there are no validation errors, udpate the specified custom user metas.

Thanks.

Related posts

Leave a Reply

1 comment

  1. add_action( 'user_profile_update_errors',array(AlumniUserRolesAndCapabilities::getInstance(),"validateCustomUserMeta"),10,3);
    

    thats the answer, the 10 and 3 are the key, it has something to do with the order of execution.

    before without those numbers, $user was not sent to the callback, but when I added those numbers at the end, the $user object was passed.

    Hope this helps anyone.