将自定义字段的多个值存储在一个用户元键中

时间:2018-08-27 作者:Alief

我在用户配置文件中添加了一些自定义字段。这是“出生日期”。

“有”month_of_birth“,”day_of_birth“,”year_of_birth“。

如何将它们存储在一个元密钥中(\'birth_date\') ?

以下是自定义字段的代码:

// 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:短代码-

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\');

2 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

“有”month_of_birth“,”day_of_birth“,”year_of_birth“。

如何将它们存储在一个元密钥中(\'birth_date\') ?

如果要用一个元键替换所有三个元键,则可以保存birth_date 元作为array, 所以值应该是这样的:

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

Here are the steps:

<在birthdate_show_extra_profile_fields(), 检索birth_date 元值如下:

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 );
    ?>
然后更换name 下拉菜单的。select字段),并替换用于检索个人生日、日期和年份的代码:

对于出生月份菜单:

<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
对于出生日菜单:

<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
对于出生年份菜单:

<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
然后使用此选项保存birth_date 元值:

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 );
}
更新您可以检索保存的birth_date 元值如下:

// 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
如果希望月份名称以大写开头,请应用ucfirst() 在月份名称上,如下所示:

echo ucfirst( $birth_date[\'month\'] ); // e.g. August
所以你的user_birthdate_info_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\', 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). 所以在birthdate_show_extra_profile_fields() 函数,更改如下:(需要重新缩进代码)

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 ) :
收件人:

foreach ( array(
    \'\',
    \'January\', \'February\', \'March\',     \'April\',   \'May\',      \'June\',
    \'July\',    \'August\',   \'September\', \'October\', \'November\', \'December\',
) as $label ) :
    $value = $label; // use the label as the value
所以你的user_birthdate_info_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\', true);

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

SO网友:Baikare Sandeep

您可以将这些字段保存在单用户元字段中,如下所示:


 // 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 ) ); 
}

注意:代码中的注释很重要。

拜卡尔·桑德普,你好。

结束