Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Edit: Add profile field (usermeta) to user registration

I am trying to add a custom field to the Add New User page much as I have done with the user profile page like so:

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>

<h3>Player information</h3>

<table class="form-table">

    <tr>
        <th><label for="team-meta">Team Name</label></th>

        <td>
            <?php 
            $status = get_the_author_meta( 'team-meta', $user->ID ); 
            $items = get_posts (array (
                'post_type' => 'team_page',
                'posts_per_page' => -1
            ));
            echo '<select name="team-meta" id="team-meta">  
                <option value="">No team selected</option>'; 
             foreach($items as $item) {

            echo '<option value="'.$item->ID.'"',$status == $item->ID ? ' selected="selected"' : '','>'.$item->post_title.'</option>';  
                } // end foreach  
            ?>

            <span class="description">Please enter the player's team name </span>
        </td>
    </tr>

</table>
<?php }

add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );

function my_save_extra_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_users', $user_id ) )
    return false;

update_user_meta( $user_id, 'team-meta', $_POST['team-meta'] );
}

Basically I want whoever creates a new user to be able to add this directly without going to the user profile.

share|improve this question
    
Your code works. The field appears in the form and the data is saved to the DB, though you are doing no data validation or sanitization. –  s_ha_dum Oct 4 '13 at 9:15
    
How odd. This is on the page user-new.php right? Eventually only admin will be able to use this field to create new users but for now I can't even get it to show up. Its just in functions.php and everything else there works... –  mantis Oct 4 '13 at 9:21
    
No. You've added the field to the registration form at wp-login.php?action=register, which by all indications seemed to be the point. –  s_ha_dum Oct 4 '13 at 9:24
    
Aha! Yes, your right. I thought they were the same thing. I guess that was a pretty dumb question then ;( Any idea how to hook to user-new.php? I suppose I should edit the question. –  mantis Oct 4 '13 at 9:30
add comment

1 Answer

up vote 1 down vote accepted

To add data to the form at user-new-php, I believe you want the user_new_form hook. Unfortunately, it is marked "@since 3.7.0" so it isn't in the stable release yet.

I am tempted to tell you to hack it into your site in exactly the place that it will soon appear, but that would be wrong. :) You will have to wait, or install the subversion release.

share|improve this answer
    
That explains why there are no articles on the subject then ;) Maybe I'll test the 3.7 beta locally so at least I can have it ready. Looks like its scheduled for release pretty soon though :) –  mantis Oct 4 '13 at 9:47
    
Not having much luck using the hook. But at least its displaying on the right page. :) –  mantis Oct 25 '13 at 21:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.