1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* Add the post form to the user/profile edit page in the admin. */
add_action( 'show_user_profile', 'ypf_user_profile_video' );
add_action( 'edit_user_profile', 'ypf_user_profile_video' );

/* Function for displaying an extra form on the user edit page. */
function ypf_user_profile_video( $user ) {

    /* Get the current user's favorite post. */
    $ypf_video = get_user_meta( $user->ID, 'ypf_video', true );
    ?>

    <h3>Youtube Video</h3>

    <table class="form-table">

        <tr>
            <th><label for="ypf_video">Youtube video</label></th>

            <td>
                <input type="text" name="ypf_video" id="ypf_video" value="<?php echo esc_attr( $ypf_video ); ?>" />
                <span class="description">Select your Youtube video, use the ID</span>
            </td>
        </tr>

    </table>
<?php }

/* Add the update function to the user update hooks. */
add_action( 'personal_options_update', 'ypf_user_profile_video_update' );
add_action( 'edit_user_profile_update', 'ypf_user_profile_video_update' );

/* Function for updating the user's favorite post. */
function ypf_user_profile_video_update( $user_id ) {

    /* Check if the current user has permission to edit the user. */
    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    /* Only accept numbers 0-9 since it's a post ID. */
    $ypf_video = strip_tags( $_POST['ypf_video'] );

    /* Update the user's favorite post. */
    update_user_meta( $user_id, 'ypf_video', $ypf_video );
}