Recently I was having an issue with the WordPress autosave running on my posts in a custom post type. I am running a lot of scripts in the “save_post” action and couldn’t find a good way to exclude autosaves from running this code.
Note that I looked at the info being passed to the functions and the usual suspects for looking at wether it was an autosave or not weren’t helping.
My biggest problem came when typing a title in the custom post type, then clicking outside of the title field. Normally this kicks off the autosave feature automatically so the post permalink can be established and displayed below the post title. Since I wasn’t using this custom post type on the front of end of the site, I really didn’t care about that.
So now for the easiest way to disable autosave for the custom post type:
function seans_admin_init() {
if (!empty($_GET['post']) || (!empty($_GET['post_type']) && $_GET['post_type'] == '[custom_post_type]')) {
$post_type = '';
if (!empty($_GET['post'])) {
$post_type = get_post_type(intval($_GET['post']));
}
else if (!empty($_GET['post_type'])) {
$post_type = $_GET['post_type'];
}
if (!empty($post_type) && $post_type == '[custom_post_type]') {
// Hi, I block Autosave from running on posts
wp_deregister_script('autosave');
}
}
}
add_action('admin_init', 'seans_admin_init');
Look at admin-ajax.php on line 956.
Ok, so I missed something. Sue me. This blocks the Autosave from running completely, so I don’t have to trust that the variable will be caught. Also reduces the amount of code in If’s.
Huh?
Just look fir that constant when registering your actions. Don’t register the problematic actions when autosave is running.