Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need a way to disable the save process completely using an action/filter. Something that works (for e.g.:) in the query, posts_clauses or wp_insert_post/save_post/update_post hooks.

So far I only tried to return '';, which gives me tons of errors for missing values for post object parts in the admin UI.

This should happen "silently", so no errors get thrown when php_error/WP_DEBUG and such are set to TRUE/On.

Btw: I'm not asking for how to disable the autosave feature.

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted
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.

share|improve this answer
3  
Or add_filter('wp_insert_post_empty_content', '__return_true'); –  chrisguitarguy May 12 '12 at 21:46
    
I guess you haven't tried this? The problem is that I get tons of Notice: Undefined property: stdClass::$post_XY in R:\development\xampp\htdocs\wprepo\wp-admin\edit-form-advanced.php on line XY and Warning: Cannot modify header information - headers already sent by (output started at R:\development\xampp\htdocs\wprepo\wp-admin\post-new.php:46) in R:\development\xampp\htdocs\wprepo\wp-includes\functions.php on line 86X. It's exactly the same result as intercepting the query filter and returning ''/false/null. –  kaiser May 13 '12 at 13:18
    
Ok, here's why I am wrong: I checked the 2nd argument inside the filter (see edit of your answer). There I checked if the post_status is draft/pending/auto-draft. The last one is a real problem maker, as on every page-new re-load an auto-draft gets added to the database (which will be cleaned up by wp automagically). If I disable the save process there, then the above mentioned errors jump in. @ChristopherDavis I don't really understand why it works without priority and argument number in your case. –  kaiser May 13 '12 at 14:06
    
@kaiser Use of the wp_insert_post_empty_content filter is indeed problematic with auto-draft(s) and thus should be used with caution on knowledge of the caveat as explained in my answer. –  hexalys Apr 5 at 4:56
add comment

The reason you get notices for stopping inserts with the wp_insert_post_empty_content filter as mentioned in your comment at the http://wordpress.stackexchange.com/a/51980/31794 answer, is that: For post-new.php the auto-draft process needs to get a $post->ID through get_default_post_to_edit() and wp_insert_post(), and use that ID from the $post return. i.e. The 'Add New Post' pages actually creates and gets a new 'post record' every time.

Sadly wp_insert_post() return 0 if you stop the save process instead of an expected post ID. In other words, you can't stop 'Auto drafts' with the 'wp_insert_post_empty_content' filter. And should you use the filter, you sadly must let "auto-drafts pass through to avoid the PHP Notice. It's a pretty bad bug.

The only way I have found left to stop creating new auto-draft records pointlessly and go around this bug is, to extend the wpdb class with a db.php Drop-in plugin:

class wpdb_ext extends wpdb
{
  function insert($table, $data, $format = null) {
    if (isset($data['post_status']) && $data['post_status'] === "auto-draft" && ($pa = (int)$data['post_author'])
        && ($id = $this->get_var("SELECT ID FROM $table WHERE post_status = 'auto-draft' AND post_author = $pa LIMIT 1"))){
        //fake insert id and return id of existing auto-draft as base for New post page.
        return $this->insert_id = (int)$id;
    }
    return parent::insert($table, $data, $format = null);//else do actual insert
  }
}
$wpdb = new wpdb_ext(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);//overload wpdb

This keeps only one auto-draft per author, and avoids pointless new auto-draft(s) records wasting/skipping id increments for nothing.

share|improve this answer
    
Link to wordpress bug report for reference: core.trac.wordpress.org/ticket/27683 –  hexalys Apr 5 at 21:19
    
Be aware this will break plugin which rely on distinct IDs. Multilingual Press for example allows translations right from the post new screen, it needs a new, unique ID to do that. Auto-drafts are deleted after a while, so there is no harm creating them. –  toscho Apr 6 at 23:24
    
@toscho What I am putting here does not prevent the return of a 'correct' ID. There is strictly no need to constantly create blank records is they can't be reused. That's an anti-pattern AFAIC. The Auto-drafts records are completely blank. Why would you need a new blank record every time when you already have one available? I'll look into Multilingual Press though. –  hexalys Apr 7 at 19:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.