Tag Info

Hot answers tagged

21

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


15

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


11

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


8

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


8

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


8

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


7

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


7

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


7

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


6

TL;DR: No, you can use save_post. Unless you consider programmatic modification of only metadata "modifying a post". There (theoretically) is a way of modifying a post without triggering the save_post action hook: Direct modification of the database. But for one no plugin or theme author in his right mind would go that route and for another it would ...


6

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.


6

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


5

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


5

This might work for you as well. It scans the post, then pulls in the image set as 'featured'. If that doesn't exist, it returns the first image attached to the post. If there isn't one, it looks for any image in the post; if that's not there, it grabs a YouTube screenshot. Put this in your theme's functions.php file: // Note that your theme must support ...


5

Checking if meta values exist You can use get_posts() (or the WP_Query object) to query all posts which match the post meta you want to save. You will need to specify the post status (i.e. which statuses are you willing to ignore). Below is the untested code to this. (For completeness I've left in arguments which could be omitted because they are given ...


5

Following are the issues that were preventing from the meta values to be saved in both of your save functions. 1. Incorrect post_type checking. You are checking post type to be of 'page' in your save function where as your meta boxes are being displayed on your custom post type of 'film'. Your code: if ( 'page' == $_POST['post_type'] ) { if ( ...


4

I ended up just checking for the existence of a custom value prior to setting it. That way, if it's a newly created post the custom value would not yet exist. function attributes_save_postdata($post_id) { if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (!wp_verify_nonce($_POST['_attributes_noncename'], plugin_basename(__FILE__))) ...


4

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.


4

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


4

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


4

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


4

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


4

Do: add_action('save_post', 'my_save_function', 10, 2); And the $post object will be passed as second argument to your function: function my_save_function($post_ID, $post) {


4

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


4

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


4

Based on pospi's suggestion to use transients, I came up with the following. The only problem is there is no hook to put the message below the h2 where other messages go, so I had to do a jQuery hack to get it there. First, save the error message duing your save_post (or similar) handler. I give it a short lifetime of 60 seconds, so it is there just long ...


4

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


3

save_post is too late. Look at the source and you can see that the post has already been saved when that hook fires. You will have to interrupt the process well before that hook if you want to prevent saving. I think I would lean toward something like this: add_filter( 'post_updated_messages', function($messages) { $messages['post'][11] = 'This is ...


3

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


3

You need to match the $priority you used to hook the action: remove_action( 'save_post', array( $this, 'save_box' ), 20 /* Same as add_action call */ ); http://codex.wordpress.org/Function_Reference/remove_action Make sure you take the $post_id argument in your save_box method too: function save_box( $post_id ) { ... }



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