When using the Gutenberg Block Editor the normal hooks for saving a post don't have the same behavior as with the classic editor.

For example, if you hook into the save_post-hook the $permalink and the $categories will not return the new value, but instead the old value. Which is not the behavior that I expected from the classic editor.

add_action( 'save_post', 'custom_save_post', 10, 3 );

function custom_save_post( $post_id, $post, $update ) 
  $permalink = get_permalink( $post_id ); 
  $categories = get_the_terms( $post_id, 'category' );

How can I make it work so that I can retrieve the pre-updated permalink and the post-updated permalink?

  • 1
    I can only tell you that all my test text files contain the proper contents - the correct/updated permalink in permalink.txt and the correct/updated category objects dump in categories.txt and terms.txt. So I didn't need to proceed to the fourth step (editing the core taxonomy.php file). Are you using the latest WordPress stable release? Have you tested on other hosts? This might be something specific to your host/server..
    – Sally CJ
    Feb 28 '20 at 18:13
  • @SallyCJ I really appreciate you taking the time to test. Thanks for your comments. It's weird because it happend on my local computer but also on two different remote hosts. But it helps a lot that you did not notice anything, perhaps I should try a different computer.
    – Mark
    Feb 28 '20 at 18:37
  • 1
    Ok, but I should mention that I tested only with the block editor enabled (i.e. not tested on the classic editor) and if you haven't, try testing the Quick Edit link on the Posts -> All Posts page.
    – Sally CJ
    Feb 28 '20 at 19:00
  • I am also using the block editor. Great tip about the Quick Edit link. The problem does not occur there. Could you confirm one more thing. If you edit the post the "Update" button will become clickable again after some duration right? Because mine is stuck even on fresh host, fresh wordpress, default theme. When it's stuck the bug appears on the full post (not the quick edit).
    – Mark
    Feb 28 '20 at 21:32
  • 1
    So I kinda did expect you'd ask that question, and the answer is, the button works just fine for me - no "stuck-ing". So try to inspect the network - on Chrome, Ctrl + Shift + I then go to the Network tab and just check the relevant requests. In fact, the console might be able to tell you what's going on with the button getting stuck..
    – Sally CJ
    Feb 28 '20 at 21:42

When using the Gutenberg Block editor you need to use different hooks to get the expected behavior.

credits: @SallyCJ

  • Issue reported on Gutenberg Github (github.com/WordPress/gutenberg/issues/20550)
    – Mark
    Feb 29 '20 at 0:57
  • 2
    I tested on a fresh WordPress install with no plugins and using the default theme and with the default Custom Fields editor being disabled, and I actually got the same results as originally mentioned in the question (old permalink & categories) when using the save_post hook. So I'm not sure if that's really a bug (but surely the WordPress core should have/give a reasonable explanation for that), but if you use the rest_after_insert_<post type> hook, you'd get the proper permalink & categories. +1, btw :)
    – Sally CJ
    Feb 29 '20 at 11:02
  • So I think I may know why the save_post hook is giving the old permalink and categories - If you look at WP_REST_Posts_Controller::update_item(), the function calls wp_update_post() which then calls wp_insert_post() which fires hooks like save_post and wp_insert_post, but that update_item() only updates the categories/terms after wp_update_post() is called, hence for permalinks that rely upon the categories, the permalink would only be updated after the categories are updated.
    – Sally CJ
    Feb 29 '20 at 22:39
  • Having said that, it's probably not a bug.. but we just have to use another hook for post being created or updated via the REST API. But even if so, the WordPress core should mention that somewhere... :)
    – Sally CJ
    Feb 29 '20 at 22:45
  • 1
    I still believe if something does not behave as is expected it can be considered a bug. I take the classic editor and documentation as the origin. Gutenberg which does fire the same hooks, should also use it in the same way and not a different way 'cause that is the confusion we have right now. Different entry points, different results. Anyway I can finally continue with the project =)
    – Mark
    Mar 1 '20 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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