Support

Account

Home Forums General Issues Read & Understood toggle for logged in users

Solving

Read & Understood toggle for logged in users

  • Hi. I’m trying to make a simple “read & understood” toggle with ACF true/false, for posts. It’s for a site only accessible for logged in users. The site works as an internal system for memos, updates on internal processes, and such.

    Basically I want to create a frontend form with a true/false field. When saved I want to store user IDs in a meta field, as an array, attached to the post. With that data I can easily show who read the post.

    Alternatively you could go the other way around this, and relate the post ID to the user ID, where the data is saved to the user profile, instead of user IDs stored on the post. Hope this makes sense!

    Should be quite simple, but I really can’t get my head around it. Any advice and help is highly appreciated. Thanks guys.

  • First, this would not be saved as a single ACF field. The user IDs for the people that have toggled the “Read & Understood” field would be save into a standard WP custom meta field that allows multiple values. The ACF true/false field would simply be a mechanism to set values in this other field.

    From here your would create an acf/prepare_field filter for the true false field. This filter would look at the array of user IDs stored in the WP meta field to see if this user is present. If it is then set the value to 1 (true) if not then set the value to 0 (false). ACF will save this field to the post, but we really don’t care what it saves because the actual value will be determined by the values stored elsewhere.

    The next step is to create an acf/save_post action with a priority of < 10. We want this to run before ACF saves the value and we want to look at $_POST[‘acf’][$field_key]. The reason we want to look here is because this is the submitted value and will be unaffected if 2 people happen to submit the form at exactly the same time. If we did this on the saved value we’d get whatever the last person submitted, which would be fine as long as you can guarantee 2 people can’t submit the form at the same time, which would be nearly impossible.

    In this acf/save_post action you would get the submitted value. If it is “1” then you would add this user ID to the WP meta field if they are not already present. If it is “0” then you would remove them from the list if they exist.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.