1

I made custom profile fields for birthday data.

        function my_user_field( $user ) {
            $birthday_day = esc_attr( get_the_author_meta( 'birthday_day', $user->ID ) );
            $birthday_month = esc_attr( get_the_author_meta( 'birthday_month', $user->ID ) );
            $birthday_year = esc_attr( get_the_author_meta( 'birthday_year', $user->ID ) );
        ?>

            <h3><?php _e('More About You'); ?></h3>
            <table class="form-table">                   
                <tr>
                    <th>
                        <label for="birthday"><?php _e('Birthday'); ?>
                    </label></th>
                    <td>
                      <span class="description"><?php _e('When is your birthday?'); ?></span><br>
                        <form action="">
                        <select name="birthday_day" id="birthday_day">
                            <?php
                            for ($i=1; $i<=31; $i++)
                            {
                                echo '<option value="';
                                echo $i;
                                echo '"';
                                 if ($birthday_day == $i ) { 
                                     echo 'selected';
                                    }
                                echo' >';
                                echo $i;
                                echo'</option>';
                            };
                            ?>
                        </select>
                        <select name="birthday_month" id="birthday_month" onchange="updateDays();">
                            <option value="1" <?php if ($birthday_month == '1') { ?>selected<?php }?> >January</option>
                            <option value="2" <?php if ($birthday_month == '2' ) { ?>selected<?php }?> >February</option>
                            <option value="3" <?php if ($birthday_month == '3' ) { ?>selected<?php }?> >March</option>
                            <option value="4" <?php if ($birthday_month == '4' ) { ?>selected<?php }?> >April</option>
                            <option value="5" <?php if ($birthday_month == '5' ) { ?>selected<?php }?> >May</option>
                            <option value="6" <?php if ($birthday_month == '6' ) { ?>selected<?php }?> >June</option>
                            <option value="7" <?php if ($birthday_month == '7' ) { ?>selected<?php }?> >July</option>
                            <option value="8" <?php if ($birthday_month == '8' ) { ?>selected<?php }?> >August</option>
                            <option value="9" <?php if ($birthday_month == '9' ) { ?>selected<?php }?> >September</option>
                            <option value="10" <?php if ($birthday_month == '10' ) { ?>selected<?php }?> >October</option>
                            <option value="11" <?php if ($birthday_month == '11' ) { ?>selected<?php }?> >November</option>
                            <option value="12" <?php if ($birthday_month == '12' ) { ?>selected<?php }?>>December</option>
                        </select>
                        <select name="birthday_year" id="birthday_year" onchange="updateDays();">
                            <?php 
                            for ($i=2010; $i>1930; $i--)
                            {
                                echo '<option value="';
                                echo $i;
                                echo '"';
                                 if ($birthday_year == $i ) { 
                                     echo 'selected';
                                    }
                                echo' >';
                                echo $i;
                                echo'</option>';
                            };
                             ?>
                        </select>

        </form>
                    </td>
                </tr>

            </table>
        <?php 
        }


        function my_save_custom_user_profile_fields( $user_id ) {
            if ( !current_user_can( 'edit_user', $user_id ) )
                return FALSE;

            update_usermeta( $user_id, 'birthday_day', $_POST['birthday_day'] );
            update_usermeta( $user_id, 'birthday_month', $_POST['birthday_month'] );
            update_usermeta( $user_id, 'birthday_year', $_POST['birthday_year'] );  
        }

        add_action( 'show_user_profile', 'my_user_field' );
        add_action( 'edit_user_profile', 'my_user_field' );
        add_action( 'personal_options_update', 'my_save_custom_user_profile_fields' );
        add_action( 'edit_user_profile_update', 'my_save_custom_user_profile_fields' );

And it normaly saves to db, but there is a fail. I can choose for example "31 of February" and this is bad. How can I make any verification for these? I want if user selected "31 of February" and submited form, then his date would be saved as "28 of February", but if selected year is leap year, then "29 of February".

I know that is posible to make more flexible form using javascript, but how to make this using PHP?

1

This doesn't really seem to be a WP question, but: as far as PHP goes, the function you're looking for is checkdate(). That said, you really should also do something client-side (that is, JS) for the user.

  • Thank you. It's really useful. But how I can get checked values before submitting the form? – okonik Jul 16 '13 at 13:01
  • Before submission is client-side, thus JavaScript. – helenhousandi Jul 16 '13 at 14:17

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.