ACF update_field associated with a User

I’ve created some custom fields on the user profile page in WordPress.

I’ve managed to build a front end editor for the user to change some e-mail preferences:

Read More
<?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>

Now, this actually seems to work. If I update a checkbox it shows and checked/unchecked as it should, and it shows correctly in the front end and the back end.

But, when I try to query this setting with a wp_query to actually send the e-mails to those who haven’t opted out, it bug out a little.

If the user opts out, then opts back in, the wp_query doens’t pick them up. It only picks them up when I go into the wp-admin area and update their user profile. I don’t actually have to change anything, just open the user up and click update.

Here’s the wp_query just incase:

<?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.

Any ideas how I can get this working properly? Is there a function to fake a click on the ‘Update User’ button in wp-admin?

Thanks.

Related posts

Leave a Reply

1 comment

  1. When the user opts out, the $field value will be empty, and therefore the value-attribute of the checkboxes will be empty. I haven’t fully tested it, but this can yield unexpected behaviour in your setup. When a form with an unchecked checkbox is submitted through a POST request, the checkbox name will not be set in the $_POST array, which is why you should, in this case, set the value-attribute of the checkboxes to “1”, so it is properly stored via update_field.

    Could you try changing the checkbox values in the code posted above to “1” for the checkbox input elements? That would be value="1" instead of value="<?php echo $field; ?>".

    To prevent PHP notices being generated for inexistent array keys, I would advise to change

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

    to

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

    as well.