Support

Account

Home Forums General Issues Check if someone checked a checkbox

Helping

Check if someone checked a checkbox

  • Hi,

    I’ve got a simple yes/no checkbox. Is it possible to check if someone checked this box in the last 24 hours?

  • You can use the acf/save_post action to check if the checkbox is checked and save the current time to the post meta. Then you can check the current time against the saved time in your template.

    Save the current time when the post is saved:

    
    <?php
    
    function my_acf_save_post( $post_id ) {
    	// Your checkbox field.
    	$checkbox_field = get_field( 'checkbox', $post_id );
    
    	// The post meta you'll be creating.
    	$checkbox_time = get_post_meta( $post_id, 'checkbox_saved_time', true );
    
    	/*
    	 * Add the current time to the post meta if your checkbox field is checked
    	 * and checkbox_saved_time doesn't already have a value saved.
    	 */
    	if ( $checkbox_field && ! $checkbox_time ) {
    		update_post_meta( $post_id, 'checkbox_saved_time', strtotime( 'now' ) );
    	}
    }
    
    add_action( 'acf/save_post', 'my_acf_save_post', 20 );
    
    ?>
    

    Compare the time in your template:

    
    <?php
    
    $checkbox_time = get_post_meta( $post_id, 'checkbox_saved_time', true );
    
    if ( $checkbox_time < strtotime( '-24 hours' ) ) {
    	// The checkbox was checked within the last 24 hours.
    }
    
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Check if someone checked a checkbox’ is closed to new replies.