Last active
February 21, 2024 21:34
-
-
Save igorbenic/6f068f6b0773ffbbf0ddbde47cc496d4 to your computer and use it in GitHub Desktop.
Automation on Post Publish | https://www.ibenic.com/automation-post-publish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'publish_future_post', 'automation_on_publish', 20, 1 ); | |
/** | |
* Create automate tasks on post publish. | |
* | |
* @param integer $post_id Post ID that is published. | |
*/ | |
function automation_on_publish( $post_id ) { | |
$post = get_post( $post_id ); | |
if ( empty( $post ) ) { return; } | |
// Let's be sure that we are checking against our post type. | |
if ( 'our_post_type' !== get_post_type( $post ) ) { return; } | |
// This post has probably been published already. | |
if ( 'future' != $post->post_status && 'publish' != $post->post_status) { return; } | |
$time = strtotime( $post->post_date_gmt . ' GMT' ); | |
// Nope, we still won't run. WP will reschedule it so we're fine. | |
if ( $time > time() ) { | |
return; | |
} | |
// let's do something on the automation. | |
// or just create a hook so we can be sure when hooking that we're be fine. | |
do_action( 'automation_on_publish', $post ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'transition_post_status', 'automation_on_post_status', 10, 3 ); | |
/** | |
* Automating on general hook. | |
*/ | |
function automation_on_post_status( $new_status, $old_status, $post ) { | |
if ( 'publish' === $new_status && 'future' === $old_status ) { | |
// Do something if the post has been transitioned from the future (scheduled) post to publish. | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'future_to_publish', 'automation_on_future_to_publish' ); | |
/** | |
* Automating on specific status change. | |
*/ | |
function automation_on_future_to_publish( $post ) { | |
// We are sure that the post has been published from scheduled status. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'publish_my_post_type', 'automation_on_publish', 10, 2 ); | |
/** | |
* Automating on publish for post type. | |
*/ | |
function automation_on_publish( $post_id, $post ) { | |
// We are sure that our post type has been published. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// wp-includes/post.php | |
function wp_transition_post_status( $new_status, $old_status, $post ) { | |
/** | |
* Fires when a post is transitioned from one status to another. | |
*/ | |
do_action( 'transition_post_status', $new_status, $old_status, $post ); | |
/** | |
* Fires when a post is transitioned from one status to another. | |
*/ | |
do_action( "{$old_status}_to_{$new_status}", $post ); | |
/** | |
* Fires when a post is transitioned from one status to another. | |
*/ | |
do_action( "{$new_status}_{$post->post_type}", $post->ID, $post ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// wp-includes/default-filters.php | |
add_action( 'publish_future_post', 'check_and_publish_future_post', 10, 1 ); | |
// wp-includes/post.php | |
/** | |
* Publish future post and make sure post ID has future post status. | |
* | |
* Invoked by cron 'publish_future_post' event. This safeguard prevents cron | |
* from publishing drafts, etc. | |
* | |
* @since 2.5.0 | |
* | |
* @param int|WP_Post $post_id Post ID or post object. | |
*/ | |
function check_and_publish_future_post( $post_id ) { | |
$post = get_post($post_id); | |
if ( empty($post) ) | |
return; | |
if ( 'future' != $post->post_status ) | |
return; | |
$time = strtotime( $post->post_date_gmt . ' GMT' ); | |
// Uh oh, someone jumped the gun! | |
if ( $time > time() ) { | |
wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // clear anything else in the system | |
wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); | |
return; | |
} | |
// wp_publish_post() returns no meaningful value. | |
wp_publish_post( $post_id ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment