WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I don't want to write out something like this from 1 to 1000:

        <select name="number_pick" id="number_pick" >    
        <option value="1" <?php selected( '1', get_the_author_meta( 'number', $user->ID ) ); ?>>1</option>
        <option value="2" <?php selected( '2', get_the_author_meta( 'number', $user->ID ) ); ?>>2</option>
        <option value="3" <?php selected( '3', get_the_author_meta( 'number', $user->ID ) ); ?>>3</option>

So instead, I am trying to use a foreach statement to get from 1 to 1000. I think I am close, but I have something wrong and I don't know what it is.

            <select name="number_pick" id="number_pick" >
            <?php foreach (range(0,1000) as $number)
            echo "<option value='$number' selected( '$number', get_the_author_meta( 'number_pick', $user->ID ) ) >$number</option>"; ?>

Can anyone tell me what I am missing in the lines above?

Here is the complete code:

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

            function show_extra_profile_fields( $user ) { ?>
                <h3>Extra profile information</h3>
                <table class="form-table">
                        <th><label for="number_pick">Pick A Number</label></th>
                            <select name="number_pick" id="number_pick" >
                                <?php foreach (range(0,1000) as $number)
         echo "<option value='$number' selected( '$number', get_the_author_meta( 'number_pick', $user->ID ) ) >$number</option>"; ?>
            <?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, 'number_pick', $_POST['gender'] );
share|improve this question
This is a PHP/HTML question, not a WordPress question. – Chip Bennett Jan 26 '12 at 22:20
up vote 4 down vote accepted

you can save the 1000 conditional checks by using str_replace and your code would be much more efficient, something like this:

//create the select options
$options ='';
for($i=1;$i<=1000;$i++) {
    $options.=  '<option value="'.$i.'">'.$i.'</option>';

//get the saved data
$saved = get_the_author_meta( 'number_pick', $user->ID );
$saved = (!empty($saved))? $saved: false;
if ($saved)
    //if there is a saved data set the option to selected
    $options = str_replace('value="'.$saved.'"','value="'.$saved.'" selected="selected"',$options);
//echo out the options
echo $options;
share|improve this answer
+1 for str_replace method. – Rob Vermeer Jan 26 '12 at 22:44
Yeah, that worked awesome. Thanks, once again, Bainternet – Carson Jan 26 '12 at 22:57

Fist off, why a dropdown of 1000 items? Why not using a textinput field and check if the input is between 1 and 1000?

But ok, the answer:

<?php for($number=1;$number<=1000;$number++) : ?>
    <option value="<?php echo $number; ?>" <?php selected( $number, get_the_author_meta( 'number_pick', $user->ID ); ?>><?php echo $number; ?></option>
<?php endfor; ?>

So your problem is with the saving. update_usermeta( $user_id, 'number_pick', $_POST['gender'] ); the $_POST name is gender here, shouldn't that be number_pick?

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

Btw: update_usermeta is deprecated, you should use update_user_meta() instead.

share|improve this answer
Thanks Rob. I did make the user_meta change. – Carson Jan 26 '12 at 23:12

Your Answer


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.