Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
47

When a post is updated there are some hooks that are fired: 'pre_post_update' is an action fired just before the post is updated, the argument passed are 2: $post_ID and $data that is an array of all the other database colums of the post table 'transition_post_status' is an hook fired on update, and pass 3 arguments: $new_post_status, $old_post_status and $...


37

When you click 'New Post', you're simply loading the page wp-admin/post-new.php. In doing so, WordPress will always create a new post (an 'Auto Draft') to ensure all other features (such as media uploads) and plugins work as normal, even before you actually save a draft or publish the post. And this, in turn, triggers save_post. Hence your echo. Okay, ...


22

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; ...


15

Since WordPress version 3.7. - IIRC - the save_post hook - more information about the hook and its usage at Code Reference: save_post and Codex: save_post - has a third parameter $update which can be used to determine just that. @param     int               $post_ID  ...


15

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 );


15

Maruti Mohanty’s suggestion is not bad, but it will fail. There are many core actions with a higher priority: wp-admin/menu.php: add_action('admin_menu', '_add_themes_utility_last', 101); wp-includes/admin-bar.php: add_action( 'wp_footer', 'wp_admin_bar_render', 1000 ); wp-includes/canonical.php add_action( 'template_redirect', '...


14

you can use admin_notices hook first define the notice function: function my_admin_notice(){ //print the message echo '<div id="message"> <p>metabox as errors on save message here!!!</p> </div>'; //make sure to remove notice after its displayed so its only displayed when needed. remove_action('...


14

As m0r7if3r pointed out, there is no way of preventing a post from being published using the save_post hook, since the by the time that hook is fired, the post is already saved. The following, however, will allow you to revert the status without using wp_insert_post_data and without causing an infinite loop. The following is not tested, but should work. &...


13

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 ...


13

simple add an else clause to delete the post meta if not checked and your code will do just fine, so change : if ( isset($_POST['front_event']) ) { update_post_meta($post->ID, 'front_event', $_POST['front_event']); } to if ( isset($_POST['front_event']) ) { update_post_meta($post->ID, 'front_event', $_POST['front_event']); }else{ ...


13

This simplest method would be to edit the data at the point it's inserted, rather than updating it afterwards, using wp_insert_post_data instead of save_post. This works on creating a new post or updating an existing post without change. It also avoids the danger of creating an infinite loop by triggering update_post within save_post. add_filter( '...


12

The "save_post" action is only called when we actually changed something in the post page form. If we just press the update button, without changing anything, the "save_post" action is not called. This is important if we are editing a custom post type where we had custom meta boxes. If we rely on the "save_post" action and only change stuff on ...


11

The way I perform this check (within a hooked function) is to compare the post date and modified date (in GMT for standardisation) function check_new_vs_update( $post_id ){ $myPost = get_post($post_id); $post_created = new DateTime( $myPost->post_date_gmt ); $post_modified = new DateTime( $myPost->post_modified_gmt ); if( abs( ...


11

I haven't quite got the reputation to comment so I'm adding an answer even though Stephen's is excellent and correct. It just doesn't handle instances when you want to set the priority of the action. If you set the priority when adding the action but don't specify priority when you remove it you will still get an infinite loop. add_action('save_post', '...


10

This answer [mirror] from Otto in WP Tavern, actually solves the transient problem by doing what WordPress itself does to overcome the redirect problem. Totally worked for me. The problem is that transients are there for everybody. If you have more than one user doing things at the same time, the error message can go to the wrong person. It's a race ...


10

I had the exact same need, so I wrote this function - which works. Modify it to your needs. Hope this helps. // set daily rating title function set_rating_title ($post_id) { if ( $post_id == null || empty($_POST) ) return; if ( !isset( $_POST['post_type'] ) || $_POST['post_type']!='rating' ) return; if ( wp_is_post_revision( ...


10

The problem is that the function update_field from ACF works a little bit different from update_post_meta. Can you see it? update_post_meta( $post_id, $meta_key, $meta_value, $prev_value ); update_field( $field_key, $value, $post_id ); If you use update_post_meta you should in my opinion use get_post_meta to get the meta. If you use update_field you should ...


9

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 => '', // ...


9

OK, this is finally how I ended up doing it: an Ajax call to a PHP function that does the checking, sort of inspired by this answer and using a clever tip from a question I asked on StackOverflow. Importantly, I make sure that only when we want to Publish the checking is done, so that a Draft can always be saved without the checking. This ended up being the ...


9

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 executed....


8

You can do this by hand, but WP natively does it like this for settings errors: add_settings_error() to create message. Then set_transient('settings_errors', get_settings_errors(), 30); settings_errors() in admin_notices hook to display (will need to hook for non-settings screens).


8

Try this... add_action('post_updated', 'myfunction'); function myfunction( $post_id ) { global $post; if (!file_exists("/www/foo/blog/wp-content/uploads/" . $post_id)) { mkdir("/www/foo/blog/wp-content/uploads/" . $post_id, 0777); } } NOTE: Change from save_posts to post_updated which will stop the duplicate issue as it ...


8

Also, run the slug from sanitize_title_with_dashes() through wp_unique_post_slug() to ensure that it's unique. It will automatically append '-2', '-3' etc. if it's needed.


7

One of the two IDs might be a post revision. To prevent this behaviour, I always have this checks in my save_postdata function: // Autosave, do nothing if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; // AJAX? Not used here if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) return; // Check user permissions if ( ! ...


7

It's inlineeditnonce. Check line 1185 of admin-ajax.php for details.


7

First of all you have to understand that when we update a post, wp_update_post function is called. But due to a bit not optimal design of WP core, the actual saving is processed by wp_insert_post function. See it in trac on line 3006. Ok, next lets see what is inside of wp_insert_post function. As you can see, on line 2950, save_post action is called each ...


7

Figured it out, hooking into content_save_pre: function remove_empty_lines( $content ){ // replace empty lines $content = preg_replace("/&nbsp;/", "", $content); return $content; } add_action('content_save_pre', 'remove_empty_lines');


6

Post format for the post is term of native post_format taxonomy. It can be set for the post by set_post_format() function. As with any taxonomy you can also use some deeper level function, but really there is no need. And I definitely don't recommend to try and deal with terms and taxonomies directly in database (sanity at risk). Core seems to be setting ...


6

I suggest to use sessions since this will not create strange effects when two users editing at the same time. So this is what I do: Sessions are not started by wordpress. So you need to start a session in your plugin, functions.php or even wp-config.php: if (!session_id()) session_start(); When saving the post, append errors and notices to the session: ...


6

Since nothing is being done with the return value, returning the post ID is pointless and should not be done. It only provides room for confusion. Just tried it out, the following save_post action works fine. function my_save_post($post_id) { // Stop WP from clearing custom fields on autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) ...


Only top voted, non community-wiki answers of a minimum length are eligible