最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Add New User, extra fields which are required?

matteradmin5PV0评论

I use these two functions to be able to set the first and last name when I go to Add New User:

// Add First and Last name to Add New User
function add_new_user_name_fields($user) {
  ?>
    <table class="form-table">
        <tr>
            <th><label for="first_name">First Name</label></th>
            <td>
                <input type="text" class="regular-text" name="first_name" id="first_name" />
            </td>
        </tr>
        <tr>
            <th><label for="last_name">Last Name</label></th>
            <td>
                <input type="text" class="regular-text" name="last_name" id="last_name" />
            </td>
        </tr>
    </table>
  <?php
}
add_action( "user_new_form", "add_new_user_name_fields" );

// Save First and Last name when new user is added
function save_add_new_user_name_fields($user_id) {
    if (!current_user_can('create_users'))
        return false;
    update_user_meta($user_id, 'first_name', $_POST['first_name']);
    update_user_meta($user_id, 'last_name', $_POST['last_name']);
}
add_action('user_register', 'save_add_new_user_name_fields');

This works very good, but how do I set both these fields to be required? Right now I am able to add a new user without setting anything in these fields.

Lastly, adding these two fields means that they end up on the bottom below Add the user without sending an email that requires their confirmation. Is it possible to place these two fields directly below the Username ?

Thanks!

Post a comment

comment list (0)

  1. No comments so far