Prevent autosave on a custom post type in WordPress

I have a custom post type that doesn’t use the WordPress editor or its contents externally, but every time a post is saved or programmatically created, I do write contents to the post_content field so I can search for certain data in the WordPress back-end.

The problem is that every time a user goes to edit one of those posts, the autosave will overwrite my post_content data.

The following piece of code removes the autosave script when the user is editing a post of type movie.

1
2
3
4
5
6
7
8
9
add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');
 
function my_admin_enqueue_scripts() {
  switch(get_post_type()) {
    case 'movie':
      wp_dequeue_script('autosave');
      break;
  }
}
add_action('admin_enqueue_scripts', 'my_admin_enqueue_scripts');

function my_admin_enqueue_scripts() {
  switch(get_post_type()) {
    case 'movie':
      wp_dequeue_script('autosave');
      break;
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *