2 question regarding "Add New User" as site admin

Hi,

i have 2 question regarding “Add New User” as site admin.

1. i add new field to the form when we create new user at "user-new.php" the problem is that the field data not saved to DB.

after i created new user, i go to "edit user" and see the field is empty.

any sugassins to make this work?

here is the code:

function custom_user_profile_fields($user){
    ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="company">Company Name</label></th>
            <td>
                <input type="text" class="regular-text" name="company" value="<?php echo esc_attr( get_the_author_meta( 'company', $user->ID ) ); ?>" id="company" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );

function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    update_usermeta($user_id, 'company', $_POST['company']);
}
add_action('user_register', 'save_custom_user_profile_fields');

2. when we give to site admin the option to add new users, site admin dont have the check box to add the new user without sending the user email with link activation (see the screenshot)

how can we add this option to site admin? (this option super admin have)

  • Bojan Radonic

    Hey there @Ruslan, hope you're well today.

    1. Hey I tested the code you provided and it seems that the only issue is in the line 26 and the function you've used, you should replace that line with this one:

    update_user_meta($user_id, 'company', $_POST['company']);

    In case you need it you can use the code I've used, here it is:

    add_action( 'show_user_profile', 'custom_user_profile_fields' );
    add_action( 'edit_user_profile', 'custom_user_profile_fields' );
    
    function custom_user_profile_fields( $user )
    {
        ?>
            <h3>Extra profile information</h3>
    
            <table class="form-table">
                <tr>
                    <th><label for="company">Company Name</label></th>
                    <td><input type="text" name="company" value="<?php echo esc_attr(get_the_author_meta( 'company', $user->ID )); ?>" class="regular-text" /></td>
                </tr>
            </table>
        <?php
    }
    
    add_action( 'personal_options_update', 'save_custom_user_profile_fields' );
    add_action( 'edit_user_profile_update', 'save_custom_user_profile_fields' );
    add_action( 'user_new_form', 'custom_user_profile_fields' );
    add_action('user_register', 'save_custom_user_profile_fields');
    
    function save_custom_user_profile_fields( $user_id )
    {
        update_user_meta( $user_id,'company', sanitize_text_field( $_POST['company'] ) );
    }

    2. As for your second question I managed to get this to work with adding another row to the form-table, the end result should look like this this:

    <table class="form-table">
                <tr>
                    <th><label for="company">Company Name</label></th>
                    <td><input type="text" name="company" value="<?php echo esc_attr(isset($user->ID) ? get_the_author_meta( 'company', $user->ID ) : ''); ?>" class="regular-text" /></td>
                </tr>
    
                <?php if ( !is_super_admin() ) { ?>
    	<tr>
    		<th scope="row"><label for="adduser-noconfirmation"><?php _e('Skip Confirmation Email') ?></label></th>
    		<td><label for="adduser-noconfirmation"><input type="checkbox" name="noconfirmation" id="adduser-noconfirmation" value="1" /> <?php _e( 'Add the user without sending an email that requires their confirmation.' ); ?></label></td>
    	</tr>
    <?php } ?>
            </table>

    This should allow everyone to use skip confirmation email who can add the user.

    Hope this helps.

    Best regards,
    Bojan

  • Ruslan

    Hi @Bojan,
    i tried your code but none of them seems working for me.

    i have copy your code and still the "company" filed did not saved the data to DB.

    after i have created new user and activate the user by email link
    the user appeared in the user list but the company field was empty(in edit user).

    2. i have add your code with the checkbox, but when i check this box and create new user, it still send email to user and ask him to click the link.
    all the new user i tried to create Not in the list of users.

    its seems that something missing here. maybe some DB update that will not send the email and will activate the user right away.

  • Ruslan

    Hi @Bojan,
    i think the problem is that the user not created right away. the user created after the user click on "activation link" in the email his get from wordpress.

    if you can help me to add this checkbox (as in the screenshot) i will give to website admin the option to create new user without sending email with "activation code.

    but as i said before, only add this checkbox not created the user right away! it still send the user the email with "activation link"

  • Vinod Dalvi

    Hi @Ruslan,

    Thank you for your reply.

    i have copy your code and still the "company" filed did not saved the data to DB.

    after i have created new user and activate the user by email link
    the user appeared in the user list but the company field was empty(in edit user).

    I have tested the shared updated code and found it works fine when we add the user without sending an email that requires their confirmation but when the user is created that requires their confirmation then the function save_custom_user_profile_fields is called when the user activates their account by clicking activate link in the confirmation mail at this time the $_POST doesn't contain any value including company field value so it doesn't get saved.

    2. i have add your code with the checkbox, but when i check this box and create new user, it still send email to user and ask him to click the link.
    all the new user i tried to create Not in the list of users.

    I have also tested it and got the same thing that the confirmation email is getting sent. I troubleshooted it and found the is_super_admin function in the following condition is causing this issue.

    if ( isset( $_POST[ 'noconfirmation' ] ) && is_super_admin() ) {

    The above condition written in WordPress core file forces the "Skip Confirmation Email" option to work only for super admin users.

    I think the best option for you would be to create a custom page containing custom form to add users using wp_create_user or wp_insert_user functions which will be only accessible for admin users where admin users can create new users.

    Find more information about it on the following pages.

    http://code.tutsplus.com/tutorials/creating-a-custom-wordpress-registration-form-plugin--cms-20968
    http://codex.wordpress.org/Function_Reference/wp_create_user
    http://codex.wordpress.org/Function_Reference/wp_insert_user
    http://codex.wordpress.org/Page_Templates#Custom_Page_Template

    Please advise if you have more questions.

    Best Regards,
    Vinod Dalvi

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.