If you ever have an issue with slow publish/update times when working with posts, here is a handy code snippet to add which will create an error log file on publish/update and will give you a breakdown of exactly what’s happening on save. I had a client with over 10k posts in their wordpress site and they were experiencing intermittent slowness only in the wp dashboard only when trying to either publish a new post or update an existing post – it would sometimes take 10+ seconds, sometimes 20+ seconds. I tried using the Query Monitor plugin but it wasn’t throwing any red flags on update because technically the slowness was coming on save, not on refreshing the page. Thats what lead me down the rabbit hole of this code snippet, it helped me identify exactly what were the biggest culprits and after some trial and error – found the issue was primarily related to the wp optimize plugin > page caching option. I guess it was purging cache and regenerating cache for the post every time you updated the post so after disabling that the issue was resolve. Here is the code snippet, I hope it helps:
add_action('save_post', function($post_id) {
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (wp_is_post_revision($post_id)) return;
global $wp_filter;
$start = microtime(true);
error_log("=== SAVE START: $post_id ===");
// Log all hooks attached to save_post
if (isset($wp_filter['save_post'])) {
foreach ($wp_filter['save_post']->callbacks as $priority => $hooks) {
foreach ($hooks as $hook) {
$callback = $hook['function'];
if (is_array($callback)) {
$name = is_object($callback[0]) ? get_class($callback[0]) . '->' . $callback[1] : $callback[0] . '::' . $callback[1];
} elseif (is_object($callback)) {
$name = get_class($callback);
} else {
$name = $callback;
}
error_log("Hook registered: $name (priority: $priority)");
}
}
}
add_action('shutdown', function() use ($start, $post_id) {
$duration = round(microtime(true) - $start, 2);
error_log("=== SAVE END: $post_id - Duration: {$duration}s ===");
});
}, 1);




