Submitting posts from the front end – sanitizing data

When a post is created/edited from the admin, edit_post() is called. This function simply collects all the $_POST arguments and passes them to wp_update_post(). wp_update_post() then does some more logical checks and passes the data along to wp_insert_post(). wp_insert_post() calls sanitize_post(), which does all the heavy duty sanitization. So, yes, wp_insert_post() is the correct way … Read more

Disable the post save process completely

function disable_save( $maybe_empty, $postarr ) { $maybe_empty = true; return $maybe_empty; } add_filter( ‘wp_insert_post_empty_content’, ‘disable_save’, 999999, 2 ); Because wp_insert_post_empty_content is set to true, WordPress thinks there is no title and no content and stops updating the post. EDIT: An even shorter variant would be: add_filter( ‘wp_insert_post_empty_content’, ‘__return_true’, PHP_INT_MAX -1, 2 );

How to force function to run as the last one when saving the post?

add_action has a priority parameter which is 10 by default, you can increase that to load your function late. Change add_action( ‘save_post’, ‘do_custom_save’ ); to add_action( ‘save_post’, ‘do_custom_save’, 100 ); Now the priority is to set to 100 and will load quite late and will allow other function associated to load before this function is … Read more

Action hook ‘save_post’ triggered when deleting posts

It’s probably easiest to just check the post status within your function. Untested: add_action( ‘save_post’, ‘rewrite_post’, 10, 2 ); function rewrite_post( $post_id ) { if ( ‘trash’ != get_post_status( $post_id ) ) { remove_action( ‘save_post’, ‘rewrite_post’ ); $title = preg_replace( ‘/\_/’, ‘ ‘, get_the_title( $post_id ) ); $my_post = array( ); $my_post[‘ID’] = $post_id; $my_post[‘post_title’] … Read more

$update is always true in save_post

So appreciate this is a bit late but I was having the exact same issue, the $update parameter is almost completely useless if you want to check whether it is a new post or not. The way I got around this was to compare the $post->post_date with $post->post_modified. Full code snippet below. add_action( ‘save_post’, ‘save_post_callback’, … Read more

Unable to prevent function using save_post firing twice

First, you can use this hook to target only one custom type: https://developer.wordpress.org/reference/hooks/save_post_post-post_type/ This hook (and save_post) is called the first time when you click on “new …” and then the hook is called with $update = FALSE. Then to send e-mail only when the object is updated, you can test $update like this: const … Read more