0

I'm making a profile-page so my members can update they're details trough the front-end. The problem is that the select-box named "Province" won't show the correct data when the page loads.

How can i make the select-box load the correct data when the user enters the page?

Here is the code i have in functions.php at the moment. This code works when i update the Province trough the wordpress admin.

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

function extra_user_profile_fields( $user ) { ?>
    <h3><?php _e("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="facebook"><?php _e("Facebook URL"); ?></label></th>
            <td>
                <input type="text" name="facebook" id="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e("Please enter your Facebook URL."); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="twitter"><?php _e("Twitter"); ?></label></th>
            <td>
                <input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e("Please enter your Twitter Username."); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="dob"><?php _e("Date Of Birth"); ?></label></th>
            <td>
                <input type="text" name="dob" id="dob" value="<?php echo esc_attr( get_the_author_meta( 'dob', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e("Please enter your Date Of Birth, ex: 13/06/1983"); ?></span>
            </td>
        </tr>

        <tr>
            <th><label for="city"><?php _e("City"); ?></label></th>
            <td>
                <input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e("Please enter your city."); ?></span>
            </td>
        </tr>
        <tr>
            <th><label for="province"><?php _e("Province"); ?></label></th>
            <td>
                <select class="form-control" name="province" id="province">
                    <option value="Sør-Trøndelag" <?php selected( 'Sør-Trøndelag', get_the_author_meta( 'province', $user->ID ) ); ?>>Sør-Trøndelag</option>
                    <option value="Nord-Trøndelag" <?php selected( 'Nord-Trøndelag', get_the_author_meta( 'province', $user->ID ) ); ?>>Nord-Trøndelag</option>
                </select>
            </td>
        </tr>
    </table>
<?php }

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

function save_extra_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

    update_usermeta( $user_id, 'facebook', $_POST['facebook'] );
    update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
    update_usermeta( $user_id, 'dob', $_POST['dob'] );
    update_usermeta( $user_id, 'phone', $_POST['phone'] );
    update_usermeta( $user_id, 'address', $_POST['address'] );
    update_usermeta( $user_id, 'address', $_POST['address'] );
    update_usermeta( $user_id, 'city', $_POST['city'] );
    update_usermeta( $user_id, 'province', $_POST['province'] );
    update_usermeta( $user_id, 'postalcode', $_POST['postalcode'] );
    update_usermeta( $user_id, 'gender', $_POST['gender'] );
}
?>

This is the code I have in page-profile.php. All fields looks to work fine, but the select-box wont show the correct data when page is loaded.

<?php
/*
Template Name: Update Profile
*/

$wpdb->hide_errors(); nocache_headers(); global $userdata; get_currentuserinfo();

if(!empty($_POST['action'])){

    require_once(ABSPATH . 'wp-admin/includes/user.php');
    require_once(ABSPATH . WPINC . '/registration.php');

    check_admin_referer('update-profile_' . $user_ID);

    $errors = edit_user($user_ID);

    if ( is_wp_error( $errors ) ) {
        foreach( $errors->get_error_messages() as $message )
            $errmsg = "$message";
    }

    if($errmsg == '')
    {
        do_action('personal_options_update',$user_ID);
        $d_url = $_POST['dashboard_url'];
        wp_redirect( get_option("siteurl").'?page_id='.$post->ID.'&updated=true' );
    }
    else{
        $errmsg = '<div class="box-red">' . $errmsg . '</div>';
        $errcolor = 'style="background-color:#FFEBE8;border:1px solid #CC0000;"';

    }
}
get_header(); get_currentuserinfo(); ?>
    <div class="container" style="margin-top: 30px;">
    <div id="content" role="main" class="col-md-9">

        <form name="profile" action="" method="post" enctype="multipart/form-data">
            <?php wp_nonce_field('update-profile_' . $user_ID) ?>
            <input type="hidden" name="from" value="profile" />
            <input type="hidden" name="action" value="update" />
            <input type="hidden" name="checkuser_id" value="<?php echo $user_ID ?>" />
            <input type="hidden" name="dashboard_url" value="<?php echo get_option("dashboard_url"); ?>" />
            <input type="hidden" name="user_id" id="user_id" value="<?php echo $user_id; ?>" />

                <?php if ( isset($_GET['updated']) ): $d_url = $_GET['d'];?>
                        <div class="alert alert-success">Din profil ble oppdatert</div>
                <?php elseif($errmsg!=""): ?>
                        <div class="alert alert-danger"><?php echo $errmsg;?></div>
                <?php endif;?>
            <style>
                .row {margin-bottom: 10px;}
            </style>
                    <h2>Update profile</h2>
            <div class="row">
                    <div class="col-md-4">
                        <label>Fornavn</label>
                        <input type="text" class=" form-control" name="first_name" id="first_name" value="<?php echo $userdata->first_name ?>" />
                    </div>
                    <div class="col-md-4">
                        <label>Etternavn</label>
                        <input type="text" name="last_name" class="mid2 form-control" id="last_name" value="<?php echo $userdata->last_name ?>" />
                        </div>
                     <div class="col-md-4">
                        <label>Epost <span style="color: #F00">*</span></label>
                        <input type="text" name="email" class="mid2 form-control" id="email" value="<?php echo $userdata->user_email ?>" />
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6">
                        <label>Nytt passord </label>
                        <input type="password" name="pass1" class="mid2 form-control" id="pass1" value="" />
                    </div>
                    <div class="col-md-6">
                        <label>Gjenta nytt passord </label>
                        <input type="password" name="pass2" class="mid2 form-control" id="pass2" value="" />
                    </div>
                    <div class="col-md-12">
                        <em><span style="color: #F00">*</span> påkrevde felter</em>
                    </div>
                </div>

                    <h3>Kontakt info</h3>
                    <div class="row">
                        <div class="col-md-6">
                            <label>Facebook URL</label>
                            <input type="text" name="facebook" class=" form-control" id="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $userdata->ID ) ); ?>" />
                        </div>
                        <div class="col-md-6">
                            <label>Twitter</label>
                            <input type="text" name="twitter" class=" form-control" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $userdata->ID ) ); ?>" />
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <label>City</label>
                            <input type="text" name="city" class=" form-control" id="city" value="<?php echo esc_attr( get_the_author_meta( 'city', $userdata->ID ) ); ?>" />
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <label>Fylke</label>
                            <select class="form-control" name="province" id="province">
                            <option value="Sør-Trøndelag" <?php selected( 'Sør-Trøndelag', get_the_author_meta( 'province', $user->ID ) ); ?>>Sør-Trøndelag</option>
                            <option value="Nord-Trøndelag" <?php selected( 'Nord-Trøndelag', get_the_author_meta( 'province', $user->ID ) ); ?>>Nord-Trøndelag</option>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <input type="submit" value="Update" class="btn btn-info" />
                            <input type="hidden" name="action" value="update" />
                    </div>
                </div>
        </form>
    </div><!-- #content -->

    <div class="col-md-3">
        <?php get_sidebar('right') ?>
    </div>
</div>
<?php get_footer() ?>
1

You are trying to access the meta data of the user with ID stored in $user->ID but the object $user is not defined in your code. You are also using an undefined variable $user_ID.

The quickest fix in your code would be to change this:

get_the_author_meta( 'province', $user->ID ) )

With:

get_the_author_meta( 'province', $userdata->ID ) )

Tip: turn WP_DEBUG on to get notice about those kind of errrors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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