admin管理员组

文章数量:1389772

Updated with working code here:

add_action( 'acf/save_post', 'fritlaeg_artikel_3m' );

    function fritlaeg_artikel_3m( $post_id ) {

            if( get_post_meta( $post_id, 'fritlaeg_artikel_i_3_maneder', true ) ) {

            // 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' );

    }

Initial question:

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

Scenario: 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:

<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>
</ul>
</div>
</div>
</div>
</div>

Any help or suggestions is much appreciated!

Updated with working code here:

add_action( 'acf/save_post', 'fritlaeg_artikel_3m' );

    function fritlaeg_artikel_3m( $post_id ) {

            if( get_post_meta( $post_id, 'fritlaeg_artikel_i_3_maneder', true ) ) {

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

            // Using WordPress Time Constants
            // https://codex.wordpress/Easier_Expression_of_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' );

    }

Initial question:

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

Scenario: 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
            // https://codex.wordpress/Easier_Expression_of_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:

<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>
</ul>
</div>
</div>
</div>
</div>

Any help or suggestions is much appreciated!

Share Improve this question edited Apr 6, 2020 at 20:09 Jacob Juul Petersen asked Apr 4, 2020 at 17:27 Jacob Juul PetersenJacob Juul Petersen 113 bronze badges 4
  • Missing if statement here - I guess you're looking for if ( get_post_meta( $post_id, 'fritlaeg_artikel_i_3_maneder', true ) ) ? – Sally CJ Commented Apr 6, 2020 at 6:03
  • It's a good idea Sally CJ! I already tried this and the problem with this suggestion is that when the post is initially saved without having touched the checkbox, the if ( get_post_meta( $post_id, 'fritlaeg_artikel_i_3_maneder', true ) ) is not picking up the field from the database, as the field is not generated yet I guess. So if I update the post 2 times it works. But I would like it to work on the first publish/save action. :) I think what happens is that the if statement is checked before the information is saved to the database on publish_post hook. – Jacob Juul Petersen Commented Apr 6, 2020 at 19:11
  • Just did a test to see when the ACF information is saved to the database. Turns out it runs as a separate action after the publish_post action has run. Reference: advancedcustomfields/resources/acf-save_post I will try to use this hook instead. – Jacob Juul Petersen Commented Apr 6, 2020 at 19:39
  • 1 Using acf/save_post instead of publish_post did the trick. Updated the description with the working snippet if anyone else needs it. :) Thanks Sally CJ! I did use your suggestion end the end. – Jacob Juul Petersen Commented Apr 6, 2020 at 20:02
Add a comment  | 

1 Answer 1

Reset to default 0

Working code:

add_action( 'acf/save_post', 'fritlaeg_artikel_3m' );

    function fritlaeg_artikel_3m( $post_id ) {

            if( get_post_meta( $post_id, 'fritlaeg_artikel_i_3_maneder', true ) ) {

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

            // Using WordPress Time Constants
            // https://codex.wordpress/Easier_Expression_of_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' );

    }

本文标签: advanced custom fieldsCheck if checkbox is marked on publishupdate post