Check if checkbox is marked on publish/update post


I’m trying to run a function if a checkbox is marked on the post edit page.

I have created a checkbox on all posts with ACF plugin. In my custom function I would like to verify that the checkbox has been marked, and if this is true run the function.

The idea is that when the checkbox is checked then a cron event should be set up. After a certain time the cron event will then reset the meta data of the checkbox effectively unchecking the box.

Here is my function which is working:

    /* Set up WP cron event on post publish/update */

    add_action( 'publish_post', 'fritlaeg_artikel_3m', 10, 2 );

        function fritlaeg_artikel_3m( $post_id, $post ) {

        // Missing if statement here <---   

            // Getting a UNIX Timestamp
            $timestamp = time();

            // Using WordPress Time Constants
            $timestamp_after_hour = $timestamp + 0.5 * MINUTE_IN_SECONDS;

            // Define remaining parameters
            $args = array( $post_id );
            $hook = 'fritlaegning_clear_meta_data';

            // Get the timestmap of an already scheduled event for the same post and the same event action ($hook)
            // Returns false if it is not scheduled
            $scheduled_timestamp = wp_next_scheduled( $hook, $args );

            if( $scheduled_timestamp == false ) {
                wp_schedule_single_event( $timestamp_after_hour, $hook, $args );


    /* Clear the database metadata when cron event fires */

    add_action( 'fritlaegning_clear_meta_data', 'process_clear_meta_data' );

        function process_clear_meta_data( $post_id ) {

            update_post_meta( $post_id, 'fritlaeg_artikel_i_3_maneder', '0' );


The problem is that the cron event is is set up and run on all published posts regardless of the checkbox state.

I have tried checking it with the if (isset($_POST['checkbox_name_here'])) {} without any luck.

Here is the html for the edit post-page when the box is checked:

enter image description here

<div id="acf-group_5e87343b19c4b" class="postbox  acf-postbox">
<button type="button" class="handlediv" aria-expanded="true"><span class="screen-reader-text">Vis eller skjul panel Fritlægning af artikel</span><span class="toggle-indicator" aria-hidden="true"></span></button><h2 class="hndle ui-sortable-handle"><span>Fritlægning af artikel</span><a href="..../wp-admin/post.php?post=517693&amp;action=edit" class="dashicons dashicons-admin-generic acf-hndle-cog acf-js-tooltip" title="Edit field group"></a></h2>
<div class="inside acf-fields -top">
<div class="acf-field acf-field-checkbox acf-field-5e8734782f2b2" data-name="fritlaeg_artikel_i_3_maneder" data-type="checkbox" data-key="field_5e8734782f2b2">
<div class="acf-label">
<label for="acf-field_5e8734782f2b2">Fritlæg artikel i 3 måneder</label></div>
<div class="acf-input">
<input name="acf[field_5e8734782f2b2]" type="hidden">
<ul class="acf-checkbox-list acf-bl">
<li><label class="selected"><input id="acf-field_5e8734782f2b2-Fritlæg-artikel" type="checkbox" name="acf[field_5e8734782f2b2][]" value="Fritlæg artikel" checked="checked">Fritlæg artikel</label></li>

Any help or suggestions is much appreciated!

Jacob Juul Petersen 3 years 2020-04-04T12:51:32-05:00 0 Answers 88 views 0

Leave an answer