php - 与用户关联的 ACF update_field

标签 php wordpress advanced-custom-fields

我在 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 ) ) : ?>

关于如何让它正常工作的任何想法?在 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.'');

还有。

关于php - 与用户关联的 ACF update_field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23871206/

相关文章:

php - SQL 语法错误/查询

php - Drupal View : Generate xml file

javascript - 加载网站资源时出错(wordpress)

php - 如何将 woocommerce 订单号更新到自定义 ACF 字段? (wordpress)

PHP PDO MySQL count() 预处理语句

php - 如何在 Laravel 中同步同一个属性的多个值?

css - 删除 Magicline 使导航跳动

php - WordPress 元框值显示到自定义字段

css - 使用选项页面生成动态 css (ACF)

wordpress - ACF Rest Api - 类别返回值术语对象不起作用