trigger save_post event programmatically

It’s wp_insert_post() vs. wp_update_post() – where update will ultimately also call: return wp_insert_post( $postarr, $wp_error, $fire_after_hooks ); The term “once” implies that it is being fired “afterwards”. /** * Fires once a post has been saved. * * @since 1.5.0 * * @param int $post_ID Post ID. * @param WP_Post $post Post object. * @param … Read more

How do the ‘tag’ and ‘category’ (default) taxonomies do ‘save_post’ action?

It’s informative to check out the /wp-admin/post.php file, that contains the edit_post() function that calls wp_update_post(), which is a wp_insert_post() wrapper. Here’s a skeleton for saving the assigned category terms: /** * Saving assigned category terms (skeleton) */ add_action( ‘admin_action_editpost’, function() { add_filter( ‘wp_insert_post_data’, function( $data, $parr ) { add_action( ‘save_post_post’, function( $post_ID, $post ) … Read more

Return $post_id when DOING_AUTOSAVE?

The ‘save_post’ action was added to core in 2.0, and has always been an action. Looking through the current autosave procedures, it doesn’t appear to call the ‘save_post’ action directly at any time. So the short answer is, no. There is no reason, and has never been any reason, to return any value on this … Read more

Set custom messages for post update/save

http://codex.wordpress.org/Function_Reference/register_post_type example: //add filter to ensure the text Book, or book, is displayed when user updates a book add_filter(‘post_updated_messages’, ‘codex_book_updated_messages’); function codex_book_updated_messages( $messages ) { global $post, $post_ID; $messages[‘book’] = array( 0 => ”, // Unused. Messages start at index 1. 1 => sprintf( __(‘Book updated. <a href=”https://wordpress.stackexchange.com/questions/17885/%s”>View book</a>’), esc_url( get_permalink($post_ID) ) ), 2 => … Read more

Force post slug to be auto generated from title on save

The easiest workaround could be: function myplugin_update_slug( $data, $postarr ) { if ( ! in_array( $data[‘post_status’], array( ‘draft’, ‘pending’, ‘auto-draft’ ) ) ) { $data[‘post_name’] = sanitize_title( $data[‘post_title’] ); } return $data; } add_filter( ‘wp_insert_post_data’, ‘myplugin_update_slug’, 99, 2 );

Metabox with checkbox is not updating

Here is code I have used before – the main difference looks to me that you are checking if the meta exists rather than what it’s value is to determine if it should be checked. // Checkbox Meta add_action(“admin_init”, “checkbox_init”); function checkbox_init(){ add_meta_box(“checkbox”, “Checkbox”, “checkbox”, “post”, “normal”, “high”); } function checkbox(){ global $post; $custom = … Read more

How to avoid infinite loop in save_post callback

You can remove the callback from the save_post hook, update the post and then re-add the call back to the hook. The Codex gives an example. add_action(‘save_post’, ‘wpse51363_save_post’); function wpse51363_save_post($post_id) { //Check it’s not an auto save routine if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE ) return; //Perform permission checks! For example: if ( !current_user_can(‘edit_post’, $post_id) ) … Read more