I want to make custom edit profile template without plugin

Discussion in 'WordPress' started by aijazsiddique, Nov 12, 2013.

  1. #1
    i am try to coding custom profile edit template but it is not updating meta value i can't find out where is error please help me

    page-profile.php

     <?php
    /**
    * Template Name: Edit Profile Page
    *
    */
    
        /* Get user info. */
        global $current_user, $wp_roles;
        get_currentuserinfo();
    
        /* Load the registration file. */
        require_once( ABSPATH . WPINC . '/registration.php' );
        require_once( ABSPATH . 'wp-admin/includes' . '/template.php' ); // this is only for the selected() function
    
        /* If profile was saved, update profile. */
        if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'update-user' ) {
    
            /* Update user password. */
            if ( !empty($_POST['pass1'] ) && !empty( $_POST['pass2'] ) ) {
                if ( $_POST['pass1'] == $_POST['pass2'] )
                    wp_update_user( array( 'ID' => $current_user->id, 'user_pass' => esc_attr( $_POST['pass1'] ) ) );
                else
                    $error = __('The passwords you entered do not match.  Your password was not updated.', 'mytheme');
            }
    
            /* Update user information. */
    
            update_usermeta( $current_user->id, 'first_name', esc_attr( $_POST['first_name'] ) );
    
            update_usermeta( $current_user->id, 'last_name', esc_attr( $_POST['last_name'] ) );
    
            if ( !empty( $_POST['email'] ) )
                update_usermeta( $current_user->id, 'user_email', esc_attr( $_POST['email'] ) );
    
    
            update_usermeta( $user_id, 'gender', $_POST['gender'] );
            update_usermeta( $user_id, 'height', $_POST['height'] );
            update_usermeta( $user_id, 'weight', $_POST['weight'] );
            update_usermeta( $user_id, 'coach_name', $_POST['coach_name'] );
            update_usermeta( $user_id, 'coach_phone', $_POST['coach_phone'] );
    
        if ( !$error ) {
                wp_redirect( get_permalink() );
                exit;
            }
        }
    
    // calling the header.php
        get_header();
    
    ?>
    
    <div class="container content">
    
    
        <?php if ( !is_user_logged_in() ) : ?>
    
                <p class="warning">
                        <?php _e('You must be logged in to edit your profile.', 'mytheme'); ?>
                </p><!-- .warning -->
    
            <?php else : ?>
    
                <?php if ( $error ) echo '<p class="error">' . $error . '</p>'; ?>
    
                    <form method="post" id="edituser" class="sky-form" action="<?php the_permalink(); ?>">
    
                    <header>Athlete Bio</header>
                        <fieldset>
                            <section>
    
                                <label class="label" for="first_name"><?php _e('First Name', $current_user->id ); ?></label>
                                <label class="input">
    
                                <input type="text" name="first_name" id="first_name" value="<?php the_author_meta( 'first_name', $current_user->id ); ?>" >
                                </label>
                            </section>
                            <!-- first name -->
                            <section>
                                <label class="label" for="height">Last name</label>
                                <label class="input">
                                <input type="text" name="last_name" type="text" id="last_name" value="<?php the_author_meta( 'last_name', $current_user->id ); ?>" >
                                </label>
                            </section>
                            <!-- last name -->
                            <section>
                                <label class="label" for="gender">Gender</label>
                                <div class="inline-group">
                                    <label class="radio"><input type="radio" name="radio-inline"  <?php if ($gender == 'Male' ) { ?>checked="checked"<?php }?> value="Male"><i></i>Male</label>
                                    <label class="radio"><input type="radio" name="radio-inline"  <?php if ($gender == 'Female' ) { ?>checked="checked"<?php }?> value="Female"><i></i>Female</label>
                                </div>
                            </section>
                            <!-- sex -->
                            <section>
                                <label class="label" for="height">Height</label>
                                <label class="input">
                                <input type="text" name="height" id="height" value="<?php the_author_meta( 'height', $current_user->id ); ?>">
                                </label>
                            </section>
                            <!-- Height -->
    
    
                            <section>
                                <label class="label" for="weight">Weight</label>
                                <label class="input">
                                <input type="text" name="weight" id="weight" value="<?php the_author_meta( 'weight', $current_user->id ); ?>">
                                </label>
                            </section>
                            <!-- Weight -->
    
                            <section>
                                <label class="label" for="coach_name">Coach Name</label>
                                <label class="input">
                                <input type="text" name="coach_name" id="coach_name" value="<?php the_author_meta( 'coach_name', $current_user->id ); ?>">
                                </label>
                            </section>
                            <!-- Coach Name -->
                            <section>
                                <label class="label" for="coach_phone">Coach’s Phone</label>
                                <label class="input">
                                <input type="text" name="coach_phone" id="coach_phone" value="<?php the_author_meta( 'coach_phone', $current_user->id ); ?>">
                                </label>
                            </section>
                            <!-- Coach’s Phone -->
                    </fieldset>
                        <footer>
                            <?php echo $referer; ?>
                                <input name="updateuser" type="submit" id="updateuser" class="submit button" value="<?php _e('Update', 'mytheme'); ?>" />
                            <?php wp_nonce_field( 'update-user' ', $current_user->id ); ?>
                                <input name="action" type="hidden" id="action" value="update-user" />
                                <input type="hidden" name="action" value="update" />
                                <input type="hidden" name="user_id" id="user_id" value="<?php the_author_meta( 'user_id); ?>" />
                        </footer>
                </form>
            <?php endif; ?>
    
    
    
            <div class="clearfix"></div>
    
    </div>
    
    <?php get_footer(); ?>
    PHP:
    functions.php
    <?php
    
    add_action( 'show_user_profile', 'show_extra_profile_fields', 10 );
    add_action( 'edit_user_profile', 'show_extra_profile_fields', 10 );
    
    function show_extra_profile_fields( $user ) { ?>
    
                <header>Athlete Bio</header>
                        <fieldset>
    
                            <section>
                                <label class="label" for="gender">Gender</label>
                                <div class="inline-group">
                                    <label class="radio"><input type="radio" name="radio-inline"  <?php if ($gender == 'Male' ) { ?>checked="checked"<?php }?> value="Male"><i></i>Male</label>
                                    <label class="radio"><input type="radio" name="radio-inline"  <?php if ($gender == 'Female' ) { ?>checked="checked"<?php }?> value="Female"><i></i>Female</label>
                                </div>
                            </section>
                            <!-- sex -->
                            <section>
                                <label class="label" for="height">Height</label>
                                <label class="input">
                                <input type="text" name="height" id="height" value="<?php get_the_author_meta( 'height', $current_user->ID ); ?>">
                                </label>
                            </section>
                            <!-- Height -->
    
    
                            <section>
                                <label class="label" for="weight">Weight</label>
                                <label class="input">
                                <input type="text" name="weight" id="weight" value="<?php get_the_author_meta( 'weight', $current_user->ID ); ?>">
                                </label>
                            </section>
                            <!-- Weight -->
    
                            <section>
                                <label class="label" for="coach_name">Coach Name</label>
                                <label class="input">
                                <input type="text" name="coach_name" id="coach_name" value="<?php get_the_author_meta( 'coach_name', $current_user->ID ); ?>">
                                </label>
                            </section>
                            <!-- Coach Name -->
                            <section>
                                <label class="label" for="coach_phone">Coach’s Phone</label>
                                <label class="input">
                                <input type="text" name="coach_phone" id="coach_phone" value="<?php get_the_author_meta( 'coach_phone', $current_user->ID ); ?>">
                                </label>
                            </section>
                            <!-- Coach’s Phone -->
                        </fieldset>
    
    
    
    <?php }
    add_action( 'personal_options_update', 'save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
    
    function save_extra_profile_fields( $user_id ) {
    
    
    
    if ( !current_user_can( 'edit_user', $user_id ) )
            return false;
    
        update_usermeta( $user_id, 'gender', $_POST['gender'] );
        update_usermeta( $user_id, 'height', $_POST['height'] );
        update_usermeta( $user_id, 'weight', $_POST['weight'] );
        update_usermeta( $user_id, 'coach_name', $_POST['coach_name'] );
        update_usermeta( $user_id, 'coach_phone', $_POST['coach_phone'] );
    
    
    }
    PHP:
     
    aijazsiddique, Nov 12, 2013 IP
  2. lee_day_designandrcoding

    lee_day_designandrcoding Member

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    Only had a quick look but try possible "$current_user->ID" instead of "$current_user->id"
     
  3. aijazsiddique

    aijazsiddique Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #3
    thanks for reply but $current_user->id is also not working
     
    aijazsiddique, Nov 14, 2013 IP
  4. lee_day_designandrcoding

    lee_day_designandrcoding Member

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    No use the uppercase ID,
     
  5. aijazsiddique

    aijazsiddique Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #5
    still not working i think error in only functions.php can you find out error please
     
    aijazsiddique, Nov 14, 2013 IP
  6. aijazsiddique

    aijazsiddique Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #6
    no error is not in function.php error is in page-profile.php b'coz back-end form is working but front end form not working
     
    aijazsiddique, Nov 14, 2013 IP
  7. lee_day_designandrcoding

    lee_day_designandrcoding Member

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #7
    Firstly, echo the "$current_user->ID" to see if it outputs anything,

    Check the database to see if the entries are there, they may be there but with no value, e.g. ' ' or null etc...

    In your functions.php you use, "$current_user->ID"

    But in your profile page you use "$current_user->id" it needs to be ID I think, uppercase, i'm sure php is case sensitive, but i'm not 100% on that, it's good practice to keep them the same anyway.
     
  8. lee_day_designandrcoding

    lee_day_designandrcoding Member

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #8
    "$user_id", needs to be "$user_ID"
     
  9. lee_day_designandrcoding

    lee_day_designandrcoding Member

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #9
    Right, variable names are case sensitive, meaning, you have to:

    "$user_id", needs to be "$user_ID"
    &
    "$current_user->id", needs to be "$current_user->ID"
     
  10. lee_day_designandrcoding

    lee_day_designandrcoding Member

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #10
    Line 132 has an error... "<input type="hidden" name="user_id" id="user_id" value="<?php the_author_meta( 'user_id missing code here.. ); ?>" />"
     
  11. aijazsiddique

    aijazsiddique Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #11
    i done everything as you say but meta still not update from front end :(
     
    aijazsiddique, Nov 14, 2013 IP
  12. lee_day_designandrcoding

    lee_day_designandrcoding Member

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #12
    All I can suggest is you debug it methodically.

    Echo variables at certain points in the code to make sure it is outputting what you expect it to.

    Also, instead of using the "the_author_meta( 'first_name', $current_user->id )"

    Try using "echo get_user_meta($current_user->id, 'first_name', true)" this is what I usually use
     
  13. aijazsiddique

    aijazsiddique Member

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    43
    #13
    thanks Lee,
    for your support but finally i decide to use theme my login plugin now it only i needed to some customization now it work perfect
     
    aijazsiddique, Nov 15, 2013 IP