【问题标题】:ACF update_field associated with a User与用户关联的 ACF 更新字段
【发布时间】:2014-07-15 06:59:18
【问题描述】:

我在 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中是否有伪造点击“更新用户”按钮的功能?

谢谢。

【问题讨论】:

  • 我认为update_field 为帖子元表工作,那么您为什么使用user_1 作为帖子ID?我想在这种情况下它会使用当前的帖子 ID 吗?
  • 不,您可以传递很多东西来代替帖子 ID。该字段被添加到每个用户配置文件中,因此您必须指定 user_$user_id 以便它知道需要更新哪个用户。
  • 好的,谢谢你清理那个 ;-)

标签: php wordpress advanced-custom-fields


【解决方案1】:

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

您可以尝试将上面发布的代码中的复选框值更改为复选框输入元素的“1”吗?那将是 value="1" 而不是 value="&lt;?php echo $field; ?&gt;"

为了防止为不存在的数组键生成 PHP 通知,我建议也将 update_field('planner-reminders', $_POST['planner-reminders'], 'user_'.$user_id.''); 更改为 update_field('planner-reminders', empty( $_POST['planner-reminders'] ) ? '0' : '1', 'user_'.$user_id.'');

【讨论】:

  • 太棒了!我实际上只需要按照最后的建议将代码更改为update_field('planner-reminders', empty( $_POST['planner-reminders'] ) ? '0' : '1', 'user_'.$user_id.'');。但它很有魅力!谢谢!当它让我发送它时会给予赏金:)
猜你喜欢
  • 1970-01-01
  • 2019-09-28
  • 2019-11-10
  • 2013-04-04
  • 2014-07-07
  • 2022-07-09
  • 1970-01-01
  • 1970-01-01
  • 2018-12-21
相关资源
最近更新 更多