Add new user : make the fields First Name and Last name required

If you were looking to make the fields required in the WordPress admin form for adding new user as i understood from your question, where the output is not filterable, you could basically do what you described (adding form-required) on browser side

function se372358_add_required_to_first_name_last_name(string $type) {

    if ( 'add-new-user' === $type ) {
        ?>
        <script type="text/javascript">
        jQuery(function($) {
            $('#first_name, #last_name')
                .parents('tr')
                .addClass('form-required')
                .find('label')
                .append(' <span class="description"><?php _e( '(required)' ); ?></span>');
        });
        </script>
        <?php
    }

    return $type;
}

add_action('user_new_form', 'se372358_add_required_to_first_name_last_name');

Edit

In order to achieve the same on edit, you could use code like this:

function se372358_add_required_to_first_name_last_name_alternative() {
    ?>
    <script type="text/javascript">
    jQuery(function($) {
        $('#createuser, #your-profile')
            .find('#first_name, #last_name')
            .parents('tr')
            .addClass('form-required')
            .find('label')
            .append(' <span class="description"><?php _e( '(required)' ); ?></span>');
    });
    </script>
    <?php
}

add_action('admin_print_scripts', 'se372358_add_required_to_first_name_last_name_alternative', 20);

function se372358_validate_first_name_last_name(WP_Error &$errors) {

    if (!isset($_POST['first_name']) || empty($_POST['first_name'])) {
        $errors->add( 'empty_first_name', __( '<strong>Error</strong>: Please enter first name.' ), array( 'form-field' => 'first_name' ) );
    }

    if (!isset($_POST['last_name']) || empty($_POST['last_name'])) {
        $errors->add( 'empty_last_name', __( '<strong>Error</strong>: Please enter last name.' ), array( 'form-field' => 'last_name' ) );
    }
    return $errors;
}

add_action( 'user_profile_update_errors', 'se372358_validate_first_name_last_name' );

Leave a Comment