Tag Info

Hot answers tagged

19

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


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


10

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

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

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


6

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


6

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

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


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


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

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


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

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


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

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

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

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

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

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

As @t-f pointed out in his comment to question, you have an error on checking current user capability: 'delete_published_posts'. $post_author_id simply doesn't exist. After that, for your scope probably is a better hookin the filter wp_insert_post_data instead of the action save_post: because this one run when the post was already saved / updated, so if ...


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


3

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


3

I believe the problem lies in the following code: if (isset($_POST['reviews-nonce'])){ if ( !wp_verify_nonce( $_POST['reviews-nonce'], 'reviews-nonce' ) ) return; }else{return;} Good job checking for the nonce; however, the nonce name is incorrect. In the HTML form, you specify: echo '<input type="hidden" name="reviews_noncename" ...


3

This is because the first time you go round the loop $post is the current post. But the second time you go around the loop, $post has not changed. The same thing happens the 3rd, 4th, 5th, etc Because the $post variable is the current post of that page, not the post you've just saved/inserted, the loops if statement will always be true, and an infinite loop ...


3

die is an alias of exit and from php.net: void exit ([ string $status ] ) If status is an integer, that value will be used as the exit status and not printed. Try doing the following instead. function psd_upload_kaydet($post_id) { echo $post_id; die(); // for debugging, i'll explain it } edit.


3

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 action. Of course, it doesn't hurt at all to do ...


3

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


3

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


3

Use your function with these two function read the comments in the code to understand /** * If the the image is an attachment we just set the id as the post thumbnail, * if not then we get the first image from the post content , upload it using media_sideload_image and then we set the thumbnail * * @param (int) $post_id post id * * @return Void */ ...



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