Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya
Last active December 13, 2016 02:21
Show Gist options
  • Select an option

  • Save hellofromtonya/a198f1c988a3dbbc3dda9d4e7de76c84 to your computer and use it in GitHub Desktop.

Select an option

Save hellofromtonya/a198f1c988a3dbbc3dda9d4e7de76c84 to your computer and use it in GitHub Desktop.
Check if the post's submission is an "edit" action and not a "add new post" action - WordPress
/**
* Checks if the post's submission is an "edit"
*
* Details provided in this answer on Stack Overflow
* @link http://stackoverflow.com/questions/41110437/condition-only-when-the-post-is-complete-edited-not-added-or-trash/41110996#41110996
*
* @since 1.0.0
*
* @param int $post_id Post ID to evaluate
*
* @return bool
*/
function is_post_submission_an_edit_task( $post_id ) {
if ( ! isset( $_POST['action'] ) ) {
return false;
}
if ( 'editpost' != $_POST['action'] ) {
return false;
}
if ( ! isset( $_POST['_wp_http_referer'] ) ) {
return false;
}
$referer = '/wp-admin/post.php?post=' . $post_id . '&action=edit&message=';
return $referer === substr( $_POST['_wp_http_referer'], 0, strlen( $referer ) );
}
add_action( 'post_updated', 'process_after_product_edited', 10, 3 );
/**
* Process work when the product has been edited.
*
* @since 1.0.0
*
* @param int $post_id Post ID.
* @param WP_Post $post_after Post object following the update.
* @param WP_Post $post_before Post object before the update.
*/
function process_after_product_edited( $post_id, $post_after, $post_before ) {
if ( ! is_post_being_updated( $post_id ) ) {
return;
}
if ( $post_after->post_type != 'product' ) {
return;
}
// now you can do your work
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment