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&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&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 |1 Answer
Reset to default 0Working 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
版权声明:本文标题:advanced custom fields - Check if checkbox is marked on publishupdate post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744592147a2614557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Missing if statement here
- I guess you're looking forif ( get_post_meta( $post_id, 'fritlaeg_artikel_i_3_maneder', true ) )
? – Sally CJ Commented Apr 6, 2020 at 6:03if ( 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