如何在前台保存用户配置文件中的选择元数据

时间:2014-10-05 作者:onehitwonder

嗨,有人能帮我保存在这个选择框中选择的帖子标题吗?我想让人们从下拉选择框中选择帖子,并将值保存到他们的个人资料中。

非常感谢您的帮助!

add_action( \'show_user_profile\', \'product_selection_field\', 5 );
add_action( \'edit_user_profile\', \'product_selection_field\', 5 );
function product_selection_field( $user ) {
$args     = array( \'post_type\' => \'product\', \'product_tag\' => \'partner\' );
$products = get_posts( $args );
foreach ( $products as $page ) {
            if ( strtolower($product_name) == $page ) {
                $role_option .= "<option value=\'".strtolower($page->post_title)."\' selected=\'selected\'>";
                $currentrole = strtolower($role_name);
            } else {
                $product_option .= "<option value=\'".strtolower($page->post_title)."\'>";
            }

            $product_option .= $page->post_title;
            $product_option .= "</option>";

}
?>
<div class="form-group full-width">
<label for="club_choice"><?php _e("Select your club"); ?></label>
    <select name="club_choice" id="club_choice" class="input"> 
    <?php 
    echo $product_option;
    ?>
    </select>
</div>
<?php 
}

 // then I save the field
 add_action(\'user_register\',\'adding_extra_reg_fields\');
 function adding_extra_reg_fields($user_id) {
if ( !empty( $_POST[\'club_choice\'] ) )
update_user_meta( $user_id, \'club_choice\', $_POST[\'club_choice\'] );

 }

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

啊,原来它无法保持选中状态,因为我使用了“strtolower”功能来删除大写字母。这进而导致所选值与数据库中保存的值不匹配。

add_action( \'show_user_profile\', \'product_selection_field\', 3 );
add_action( \'edit_user_profile\', \'product_selection_field\', 3 );

function product_selection_field( $user ) {
if( current_user_can(\'free\') || current_user_can(\'administrator\') ) {
$user_ID = get_current_user_id();
$current_product = get_user_meta( $user_ID, "club_choice", true );
$args     = array( \'post_type\' => \'product\', \'product_tag\' => \'partner\' );
$products = get_posts( $args );

   foreach ( $products as $page ) {
    $string = $page->post_title;
    $product_option .= "<option value=\'" . $page->post_title . "\' " . selected(     $current_product, $string, false ) . ">";
    $product_option .= $page->post_title;
    $product_option .= "</option>";
}
?>
<div class="form-group full-width">
    <?php
  $user_ID = get_current_user_id();
  $all_meta_for_user = get_user_meta( $user_ID, "club_choice", true );
  echo \'<label><strong>Your current club selected is: </label>\';
  print_r( $all_meta_for_user );
  echo \'</strong><br/>\';
?>
</div>
<div class="form-group full-width">
    <label for="club_choice"><?php _e("Select your club"); ?></label>
        <select name="club_choice" id="club_choice" class="input"> 
            <?php 
        echo $product_option;
        ?>
        </select>
</div>
<?php 
}
}


// then I save the field
add_action( \'personal_options_update\', \'adding_club_reg_fields\', 10, 1 );
add_action( \'user_register\', \'adding_club_reg_fields\', 10, 1 );
function adding_club_reg_fields( $user_id ) {
    if ( isset( $_POST[\'club_choice\'] ) ) {
        update_user_meta( $user_id, \'club_choice\', $_POST[\'club_choice\'] );
    }
}

SO网友:Giovanni Putignano

以下代码有效。我补充道personal_options_update 用于保存用户配置文件的操作挂钩。在里面product_selection_field 函数您需要首先获取用户元值。此外,您还可以使用“选定”功能将选定属性添加到选项标记中。

add_action( \'show_user_profile\', \'product_selection_field\' );
add_action( \'edit_user_profile\', \'product_selection_field\' );

function product_selection_field( $user ) {
    $product_name = get_user_meta( $user->id, "club_choice", true );
    $args     = array( \'post_type\' => \'product\', \'product_tag\' => \'partner\' );
    $products = get_posts( $args );

    foreach ( $products as $page ) {
        $product_option .= "<option value=\'" . strtolower( $page->post_title ) . "\' " . selected( $product_name, $page->post_name, false ) . ">";
        $product_option .= $page->post_title;
        $product_option .= "</option>";
    }
?>
<div class="form-group full-width">
    <label for="club_choice"><?php _e("Select your club"); ?></label>
        <select name="club_choice" id="club_choice" class="input"> 
        <?php 
        echo $product_option;
        ?>
        </select>
    </div>
<?php 
}

// then I save the field
add_action( \'personal_options_update\', \'adding_extra_reg_fields\', 10, 1 );
add_action( \'user_register\', \'adding_extra_reg_fields\', 10, 1 );
function adding_extra_reg_fields( $user_id ) {
    if ( isset( $_POST[\'club_choice\'] ) ) {
        update_user_meta( $user_id, \'club_choice\', $_POST[\'club_choice\'] );
    }
}

结束

相关推荐

添加到php示例的快捷代码(带空格)

我在互联网上只找到了一小部分关于短代码的帖子,并允许它们在管理页面中工作/执行。因此,我在Wordpress中创建了几个管理页面,其中一个页面要求我从其他插件中添加一个短代码。我找到了这个解决方案,根据评论,它是有效的:do_shortcode() within Admin Page然而,答案中的示例使用了[示例]作为短代码,我尝试使用的短代码是[自定义格式=3]如果您注意到,我的短代码中有一个空格,这让我感到困惑。正因为如此,我现在正在努力将其纳入到该线程的示例中这不是一个重复的问题,我要问的是如何在提