0

I've added some custom fields into the user profile. It's "Date of Birth".

There are "month_of_birth", "day_of_birth", "year_of_birth".

How to store them in one meta key ('birth_date') ?

Here's the code for the custom field :

// ADD EXTRA PROFILE FIELD - BIRTH DATE

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

function birthdate_show_extra_profile_fields( $user ) { ?>
<h3 class="birthdate-info">Date of Birth</h3>
<table class="form-table">
    <tr>
        <th><label for="birth_date">Date of Birth</label></th>
        <td width="30%">
            <select name="month_of_birth" id="month_of_birth">
                <?php
                    $_value = trim( get_user_meta( $user->ID, 'month_of_birth', true ) );
                    foreach ( array(
                        'not-selected' => '',
                        'january' => 'January',
                        'february' => 'February',
                        'march' => 'March',
                        'april' => 'April',
                        'may' => 'May',
                        'june' => 'June',
                        'july' => 'July',
                        'august' => 'August',
                        'september' => 'September',
                        'october' => 'October',
                        'november' => 'November',
                        'december' => 'December',
                    ) as $value => $label ) :
                        $selected = selected( $value, $_value, false );
                ?>
                    <option value="<?php echo esc_attr( $value ); ?>"<?php echo $selected; ?>><?php echo esc_html( $label ); ?></option>
                <?php endforeach; ?>
            </select>
        </td>
        <td width="20%">
            <select name="day_of_birth" id="day_of_birth">
                <?php
                    $_value = trim( get_user_meta( $user->ID, 'day_of_birth', true ) );
                    foreach ( array(
                        'not-selected' => '',
                        '1' => '1',
                        '2' => '2',
                        '3' => '3',
                        '4' => '4',
                        '5' => '5',
                        '6' => '6',
                        '7' => '7',
                        '8' => '8',
                        '9' => '9',
                        '10' => '10',
                        '11' => '11',
                        '12' => '12',
                        '13' => '13',
                        '14' => '14',
                        '15' => '15',
                        '16' => '16',
                        '17' => '17',
                        '18' => '18',
                        '19' => '19',
                        '20' => '20',
                        '21' => '21',
                        '22' => '22',
                        '23' => '23',
                        '24' => '24',
                        '25' => '25',
                        '26' => '26',
                        '27' => '27',
                        '28' => '28',
                        '29' => '29',
                        '30' => '30',
                        '31' => '31',
                    ) as $value => $label ) :
                        $selected = selected( $value, $_value, false );
                ?>
                    <option value="<?php echo esc_attr( $value ); ?>"<?php echo $selected; ?>><?php echo esc_html( $label ); ?></option>
                <?php endforeach; ?>
            </select>
        </td>
        <td width="25%">
            <select name="year_of_birth" id="year_of_birth">
                <?php
                    $_value = trim( get_user_meta( $user->ID, 'year_of_birth', true ) );
                    foreach ( array(
                        'not-selected' => '',
                        '1905' => '1905',
                        '1906' => '1906',
                        '1907' => '1907',
                        '1908' => '1908',
                        '1909' => '1909',
                        '1910' => '1910',
                        '1911' => '1911',
                        '1912' => '1912',
                        '1913' => '1913',
                        '1914' => '1914',
                        '1915' => '1915',
                        '1916' => '1916',
                        '1917' => '1917',
                        '1918' => '1918',
                        '1919' => '1919',
                        '1920' => '1920',
                        '1921' => '1921',
                        '1922' => '1922',
                        '1923' => '1923',
                        '1924' => '1924',
                        '1925' => '1925',
                        '1926' => '1926',
                        '1927' => '1927',
                        '1928' => '1928',
                        '1929' => '1929',
                        '1930' => '1930',
                        '1931' => '1931',
                        '1932' => '1932',
                        '1933' => '1933',
                        '1934' => '1934',
                        '1935' => '1935',
                        '1936' => '1936',
                        '1937' => '1937',
                        '1938' => '1938',
                        '1939' => '1939',
                        '1940' => '1940',
                        '1941' => '1941',
                        '1942' => '1942',
                        '1943' => '1943',
                        '1944' => '1944',
                        '1945' => '1945',
                        '1946' => '1946',
                        '1947' => '1947',
                        '1948' => '1948',
                        '1949' => '1949',
                        '1950' => '1950',
                        '1951' => '1951',
                        '1952' => '1952',
                        '1953' => '1953',
                        '1954' => '1954',
                        '1955' => '1955',
                        '1956' => '1956',
                        '1957' => '1957',
                        '1958' => '1958',
                        '1959' => '1959',
                        '1960' => '1960',
                        '1961' => '1961',
                        '1962' => '1962',
                        '1963' => '1963',
                        '1964' => '1964',
                        '1965' => '1965',
                        '1966' => '1966',
                        '1967' => '1967',
                        '1968' => '1968',
                        '1969' => '1969',
                        '1970' => '1970',
                        '1971' => '1971',
                        '1972' => '1972',
                        '1973' => '1973',
                        '1974' => '1974',
                        '1975' => '1975',
                        '1976' => '1976',
                        '1977' => '1977',
                        '1978' => '1978',
                        '1979' => '1979',
                        '1980' => '1980',
                        '1981' => '1981',
                        '1982' => '1982',
                        '1983' => '1983',
                        '1984' => '1984',
                        '1985' => '1985',
                        '1986' => '1986',
                        '1987' => '1987',
                        '1988' => '1988',
                        '1989' => '1989',
                        '1990' => '1990',
                        '1991' => '1991',
                        '1992' => '1992',
                        '1993' => '1993',
                        '1994' => '1994',
                        '1995' => '1995',
                        '1996' => '1996',
                        '1997' => '1997',
                        '1998' => '1998',
                        '1999' => '1999',
                        '2000' => '2000',
                        '2001' => '2001',
                        '2002' => '2002',
                        '2003' => '2003',
                        '2004' => '2004',
                        '2005' => '2005',
                        '2006' => '2006',
                        '2007' => '2007',
                        '2008' => '2008',
                        '2009' => '2009',
                        '2010' => '2010',
                        '2011' => '2011',
                        '2012' => '2012',
                        '2013' => '2013',
                        '2014' => '2014',
                        '2015' => '2015',
                        '2016' => '2016',
                        '2017' => '2017',
                        '2018' => '2018',
                    ) as $value => $label ) :
                        $selected = selected( $value, $_value, false );
                ?>
                    <option value="<?php echo esc_attr( $value ); ?>"<?php echo $selected; ?>><?php echo esc_html( $label ); ?></option>
                <?php endforeach; ?>
            </select>
        </td>
    </tr>
</table>

// SAVE EXTRA PROFILE FIELD - BIRTH DATE

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

function birthdate_save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
    return false;
update_user_meta( $user_id,'month_of_birth', $_POST['month_of_birth'] );

if ( !current_user_can( 'edit_user', $user_id ) )
    return false;
update_user_meta( $user_id,'day_of_birth', $_POST['day_of_birth'] );

if ( !current_user_can( 'edit_user', $user_id ) )
    return false;
update_user_meta( $user_id,'year_of_birth', $_POST['year_of_birth'] );
}

UPDATE 2: SHORTCODE -

function user_birthdate_info_shortcode() {
if(is_user_logged_in()) {
    $user_id = get_current_user_id();
    $birth_date = get_user_meta($user_id, 'birth_date', false);

foreach($birth_date as $key => $value){
echo $value['month'] . '&nbsp;' . $value['day'] . '&nbsp;' . $value['year'];
}
}}
add_shortcode('currentuser_show_birthdate_info', 'user_birthdate_info_shortcode');
0

There are "month_of_birth", "day_of_birth", "year_of_birth".

How to store them in one meta key ('birth_date') ?

If you mean to replace all the three meta keys with one single meta key, then you can save the birth_date meta as an array, so that the value would be something like this:

array(
    'month' => 'august',
    'day'   => '27',
    'year'  => '2018'
)

Here are the steps:

  1. In the birthdate_show_extra_profile_fields(), retrieve the birth_date meta value like this:

    function birthdate_show_extra_profile_fields( $user ) {
        // Get the birth date, which is an array of month, day, and year.
        $birth_date = (array) get_user_meta( $user->ID, 'birth_date', true );
        ?>
    
  2. Then replace the name of the drop-down menus (i.e. select fields), and replace the code for retrieving the individual birth month, day, and year:

    For the birth month menu:

    <select name="month_of_birth" id="month_of_birth"> <!-- before -->
    <select name="birth_date[month]" id="month_of_birth"> <!-- after -->
    
    $_value = trim( get_user_meta( $user->ID, 'month_of_birth', true ) ); // before
    $_value = isset( $birth_date['month'] ) ? $birth_date['month'] : ''; // after
    

    For the birth day menu:

    <select name="day_of_birth" id="day_of_birth"> <!-- before -->
    <select name="birth_date[day]" id="day_of_birth"> <!-- after -->
    
    $_value = trim( get_user_meta( $user->ID, 'day_of_birth', true ) ); // before
    $_value = isset( $birth_date['day'] ) ? $birth_date['day'] : ''; // after
    

    For the birth year menu:

    <select name="year_of_birth" id="year_of_birth"> <!-- before -->
    <select name="birth_date[year]" id="year_of_birth"> <!-- after -->
    
    $_value = trim( get_user_meta( $user->ID, 'year_of_birth', true ) ); // before
    $_value = isset( $birth_date['year'] ) ? $birth_date['year'] : ''; // after
    
  3. Then use this for saving the birth_date meta value:

    function birthdate_save_extra_user_profile_fields( $user_id ) {
        if ( !current_user_can( 'edit_user', $user_id ) )
            return false;
    
        // Make sure we have a valid POSTed data, and if yes, then
        // sanitize/clean each array item.
        $birth_date = isset( $_POST['birth_date'] ) ?
            array_map( 'sanitize_text_field', $_POST['birth_date'] ) : array();
    
        update_user_meta( $user_id, 'birth_date', $birth_date );
    }
    

UPDATE

You can retrieve the saved birth_date meta value like this:

// Note that the last/third parameter is TRUE and not FALSE.
$birth_date = get_user_meta( $user_id, 'birth_date', true );

echo $birth_date['month']; // e.g. august
echo $birth_date['day'];   // e.g. 27
echo $birth_date['year'];  // e.g. 2018

If you want the month name to start in uppercase, apply ucfirst() on the month name, like this:

echo ucfirst( $birth_date['month'] ); // e.g. August

So your user_birthdate_info_shortcode() could be rewritten into this:

function user_birthdate_info_shortcode() {
    if(is_user_logged_in()) {
        $user_id = get_current_user_id();
        $birth_date = get_user_meta($user_id, 'birth_date', true);

        echo ucfirst( $birth_date['month'] ) . '&nbsp;' . // apply ucfirst()
            $birth_date['day'] . '&nbsp;' . $birth_date['year'];
    }
}

Alternatively, just save the month name (e.g. August) instead of its slug/key (e.g. august). So in the birthdate_show_extra_profile_fields() function, change this: (you'd need to re-indent the code)

foreach ( array(
    'not-selected' => '',
    'january' => 'January',
    'february' => 'February',
    'march' => 'March',
    'april' => 'April',
    'may' => 'May',
    'june' => 'June',
    'july' => 'July',
    'august' => 'August',
    'september' => 'September',
    'october' => 'October',
    'november' => 'November',
    'december' => 'December',
) as $value => $label ) :

to:

foreach ( array(
    '',
    'January', 'February', 'March',     'April',   'May',      'June',
    'July',    'August',   'September', 'October', 'November', 'December',
) as $label ) :
    $value = $label; // use the label as the value

So your user_birthdate_info_shortcode() could be rewritten into this:

function user_birthdate_info_shortcode() {
    if(is_user_logged_in()) {
        $user_id = get_current_user_id();
        $birth_date = get_user_meta($user_id, 'birth_date', true);

        echo $birth_date['month'] . '&nbsp;' . // ucfirst() not necessary
            $birth_date['day'] . '&nbsp;' . $birth_date['year'];
    }
}
  • PS: I hope you understand the <!-- before --> and <!-- after -->. But if not, the <!-- before --> means your code as it is now, and the <!-- after --> is the new code to be used. – Sally CJ Aug 27 '18 at 13:45
  • Thank you! You really saved my day. But now I have a problem with the month being all lowercase. I want the first letter to be uppercase. E.g. July instead of july. I've just included UPDATE 2 for shortcode in my question. – Alief Aug 27 '18 at 15:40
  • Please try the alternate solution in the UPDATE section of the answer. Or if you like, just use the ucfirst() method. =) – Sally CJ Aug 27 '18 at 18:14
  • Btw, you could also use echo implode( ' ', $birth_date ); instead of something like echo $birth_date['month'] . ' ' . $birth_date['day'] . ' ' . $birth_date['year'];. – Sally CJ Aug 27 '18 at 18:24
  • 1
    Thank you for your help! My code looks clean now. I've marked this as answer. =) – Alief Aug 28 '18 at 2:46
0

You can save these fields in the single user meta field like below:


 // SAVE EXTRA PROFILE FIELD - BIRTH DATE
add_action( 'personal_options_update', 'birthdate_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'birthdate_save_extra_user_profile_fields' );

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

    // Use this fields to save the selected the options
    update_user_meta( $user_id,'month_of_birth', $_POST['month_of_birth'] );
    update_user_meta( $user_id,'day_of_birth', $_POST['day_of_birth'] );
    update_user_meta( $user_id,'year_of_birth', $_POST['year_of_birth'] );


    $date_of_birth = array();
    $date_of_birth[] = sanitize_text_field( $_POST['year_of_birth'] );
    $date_of_birth[] = sanitize_text_field( $_POST['month_of_birth'] );
    $date_of_birth[] = sanitize_text_field( $_POST['day_of_birth'] );

    // Use this fields to show and perform any operations
    // Delete the birth_date if all fields are empty.
    if( empty( $date_of_birth ) && count($date_of_birth) == 0 )
        delete_user_meta( $user_id, 'birth_date' );
    else
        update_user_meta( $user_id, 'birth_date', join('/', $date_of_birth ) ); 
}

Note: Comments in the code are importants.

Regards, Baikare Sandeep.

  • Thank you. But after adding this code, there are four meta keys now in my database (birth_date, month_of_birth, day_of_birth, year_of_birth). When I delete those three meta keys, the fields get empty again. Is it normal? – Alief Aug 27 '18 at 12:53
  • Yes, in that case, you can remove the combined field as well after removing these all fields. I have updated code. Please check it. – Baikare Sandeep Aug 27 '18 at 14:08

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.