与用户关联的ACF update_field


ACF update_field associated with a User

我在WordPress的用户配置页面上创建了一些自定义字段。

我已经成功地为用户构建了一个前端编辑器来更改一些电子邮件首选项:

<?php if( isset($_POST['preferences']) ) :
    update_field('planner-reminders', $_POST['planner-reminders'], 'user_'.$user_id.'');
    update_field('event-suggestions', $_POST['event-suggestions'], 'user_'.$user_id.'');
    update_field('woa-updates', $_POST['woa-updates'], 'user_'.$user_id.'');
    echo '<p class="success">E-Mail Prefereneces Updated<p>';
endif; ?>
<form action="<?php the_permalink(); ?>" method="POST" class="user-email-settings">
    <!-- planner reminders -->
    <?php 
        $field = get_field('planner-reminders', 'user_'.$user_id.'');
        if( $field == true ) {
            $field = '1';
            $checked = true;
        }
    ?>
    <input type="checkbox" id="planner-reminders" name="planner-reminders" class="pref"
    value="<?php echo $field; ?>" <?php if($checked) :?> checked <?php endif;?> />
    <label for="planner-reminders">I don't want to revieve reminders about events I've added to my planner.</label>
    <?php $checked = false; ?>

    <!-- event suggestions from WOA -->
    <?php 
        $field = get_field('event-suggestions', 'user_'.$user_id.'');
        if( $field == true ) {
            $field = '1';
            $checked = true;
        }
    ?>
    <input type="checkbox" id="event-suggestions" name="event-suggestions" class="pref"
    value="<?php echo $field; ?>" <?php if($checked) :?> checked <?php endif;?> />
    <label for="event-suggestions">I don't want to recieve suggestions about events I may be interested in.</label>
    <?php $checked = false; ?>

    <!-- updates from WOA -->
    <?php 
        $field = get_field('woa-updates', 'user_'.$user_id.'');
        if( $field == true ) {
            $field = '1';
            $checked = true;
        }
    ?>
    <input type="checkbox" id="woa-updates" name="woa-updates" class="pref"
    value="<?php echo $field; ?>" <?php if($checked) :?> checked <?php endif;?> />
    <label for="woa-updates">I don't want to recieve e-mail updates from What's On Advisor.</label>
    <?php $checked = false; ?>
    <input type="submit" value="Save Preferences" name="preferences" id="preferences" />
</form>

现在,这似乎是有效的。如果我更新一个复选框,它会显示和勾选/未勾选,它会在前端和后端正确显示。

但是,当我尝试使用wp_query查询此设置以实际向未选择退出的人发送电子邮件时,它会出现一点错误。

如果用户选择退出,然后选择返回,则wp_query不会拾取它们。只有当我进入wp-admin区域并更新他们的用户配置文件时,它才会选择他们。我实际上不需要改变任何东西,只需要打开用户并点击更新。

下面是wp_query:

<?php $args = array(
    'role' => 'Subscriber',
    'meta_key' => 'planner-reminders',
    'meta_value' => '0',
    'meta_compare' => '=='
); ?>
<?php $user_query = new WP_User_Query( $args ); ?>
<?php if ( ! empty( $user_query->results ) ) : ?>

etc. etc.

有什么办法能让它正常工作吗?是否有一个功能来假装点击"更新用户"按钮在wp-admin?

谢谢。

当用户选择退出时,$field值为空,因此复选框的value -属性为空。我还没有完全测试过它,但这可能会在您的设置中产生意想不到的行为。当一个带有未选中复选框的表单通过POST请求提交时,复选框的名称将不会在$_POST数组中设置,这就是为什么在这种情况下,应该将复选框的value -属性设置为&;1&;,因此它可以通过update_field正确存储。

你能试着将上面代码中的复选框值更改为"1"对于复选框输入元素?这将是value="1"而不是value="<?php echo $field; ?>"

为了防止PHP为不存在的数组键生成通知,我建议修改

update_field('planner-reminders', $_POST['planner-reminders'], 'user_'.$user_id.'');

update_field('planner-reminders', empty( $_POST['planner-reminders'] ) ? '0' : '1', 'user_'.$user_id.'');