前端用户配置文件上的自定义下拉字段未保存

时间:2012-11-23 10:16:38

标签: php wordpress

我在前端用户个人资料中创建了一个下拉选项,其中包含所有自定义帖子类型的帖子。

选择它实际上并不保存选择,它只是恢复到第一个选项。

我哪里错了?

这是我在functions.php文件中的代码:

add_action( 'personal_options_update', 'save_custom_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_profile_fields' );
function save_custom_profile_fields( $user_id ) {
    update_user_meta( $user_id, 'teampage', $_POST['teampage'], get_user_meta( $user_id, 'teampage', true ) );
}

add_action( 'personal_options', 'add_profile_options');
function add_profile_options( $profileuser ) {
    $greeting = get_user_meta($profileuser->ID, 'teampage', true);
    ?><tr>
    <th scope="row">Member of which Health Board?</th>
    <td>
        <select name="teampage" id="teampage" >
            <?php $portfolioloop = new WP_Query( array( 
                'post_type' => 'board', 
                'post_status' => 'publish'
            )); ?>
            <?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>  
                <option id="Yes" <?php selected( $profileuser->teampage, 'Yes' ); ?>><?php echo the_title(); ?></option> 
            <?php endwhile; wp_reset_query(); wp_reset_postdata(); ?>
        </select>
    </td>
    </tr><?php
}

我正在使用this tutorial

1 个答案:

答案 0 :(得分:1)

我发现了你的问题。这是你犯的几个小错误。这是工作代码:

add_action( 'personal_options_update', 'save_custom_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_profile_fields' );
function save_custom_profile_fields( $user_id ) {
    update_user_meta( $user_id, 'teampage', $_POST['teampage'], get_user_meta( $user_id, 'teampage', true ) );
}

add_action( 'personal_options', 'add_profile_options');
function add_profile_options( $profileuser ) {
    $greeting = get_user_meta($profileuser->ID, 'teampage', true);
    ?><tr>
    <th scope="row">Member of which Health Board?</th>
    <td>
        <select name="teampage" id="teampage" >
            <?php $portfolioloop = new WP_Query( array( 
                'post_type' => 'board', 
                'post_status' => 'publish'
            ));
            global $post; ?>
            <?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>  
                <option <?php selected( $profileuser->teampage, $post->ID ); ?> value="<?php echo $post->ID; ?>"><?php echo the_title(); ?></option> 
            <?php endwhile; wp_reset_query(); wp_reset_postdata(); ?>
        </select>
    </td>
    </tr><?php
}

您忘记将value属性添加到每个option

此外,您不应针对所有选项检查相同的值(您使用selected( $profileuser->teampage, 'Yes' );,这实际上会检查$profileuser->teampage的值是否为Yes)。相反,我们将帖子ID分配给每个选项,我们会对此进行检查。

相关问题