Disable the post save process completely

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.

Read More

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.

Related posts

Leave a Reply

2 comments

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

    EDIT: An even shorter variant would be:

    add_filter( 'wp_insert_post_empty_content', '__return_true', PHP_INT_MAX -1, 2 );
    
  2. The reason you get notices for stopping inserts with the wp_insert_post_empty_content filter as mentioned in your comment at the https://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.