简体   繁体   中英

How to display error message when extra fields are not filled on registration in WordPress?

I would like to custamize the default user registration in Wordpress by adding first and last name and checkox with terms and coditions. The user is not able to register till all fields are filled and the checkbox is checked. I solved the checkbox with JavaScript. But now I want to add extra error messages when first and last name are not field. On the picture you see a massage for empty username and email. How could I add those kind of messages for first name and last name?

Error message for empty and email

call a function before submitting the form ie onlick of submit button:

$('#btn').on('click', function() {
   if(validateForm()){
$( "#target" ).submit();
}else{
 event.preventDefault()

}
;
});

code your custom function to check the fields validateForm();

Do you already have PHP set up for it? I'd say it is more important to have validation in PHP in case the user has javascript disabled.

http://docs.restrictcontentpro.com/article/1720-creating-custom-registration-fields https://archive.fo/369DX seems to do this... In step three, the author writes:

<?php
/**
 * Determines if there are problems with the registration data submitted
 *
 */
function pw_rcp_validate_user_fields_on_register( $posted ) {

    if ( rcp_get_subscription_id() ) {
       return;
            }

    if( empty( $posted['rcp_profession'] ) ) {
            rcp_errors()->add( 'invalid_profession', __( 'Please enter your profession', 'rcp' ), 'register' );
    }

    if( empty( $posted['rcp_location'] ) ) {
            rcp_errors()->add( 'invalid_location', __( 'Please enter your location', 'rcp' ), 'register' );
    }

}
add_action( 'rcp_form_errors', 'pw_rcp_validate_user_fields_on_register', 10 );

The thing to note is rcp_errors()->add()

The asker added:

I did it the way you suggested. I created a plugin that validates the extra fields(first and last name). Now the user is not able to register if all fields are not filled. User validation

Now the question is how could I change the error messages for user name and email address, so they can match mine? In other words where is the code that displays a message whena user does not fill in any required field?

Thank you for the help!

Can you try modifying the string in the line with rcp_errors()->add( ?

I did it the way you suggested. I created a plugin that validates the extra fields(first and last name). Now the user is not able to register if all fields are not filled. User validation

Now the question is how could I change the error messages for user name and email address, so they can match mine? In other words where is the code that displays a message whena user does not fill in any required field?

Thank you for the help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM