From 4c048f53c9d8d24c8446e5337b5a2b6deb482e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 24 Nov 2021 16:36:05 +0100 Subject: [PATCH] Turn ID into a unique slug when wp_navigation is created --- lib/navigation.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/navigation.php b/lib/navigation.php index 816e2582b8840..d23f1c5f3203f 100644 --- a/lib/navigation.php +++ b/lib/navigation.php @@ -449,3 +449,29 @@ function gutenberg_hide_visibility_and_status_for_navigation_posts( $hook ) { } add_action( 'admin_enqueue_scripts', 'gutenberg_hide_visibility_and_status_for_navigation_posts' ); + +/** + * Sets a custom slug when creating auto-draft wp_navigatino posts. + * This is only needed for auto-drafts created by the regular WP editor. + * If this page is to be removed, this won't be necessary. + * + * @param int $post_id Post ID. + */ +function gutenberg_set_unique_slug_on_create_wp_navigation( $post_id ) { + // This is the core function with the same functionality. + if ( function_exists( 'gutenberg_set_unique_slug_on_create_wp_navigation' ) ) { + return; + } + + $post = get_post( $post_id ); + if ( ! $post->post_name ) { + wp_update_post( + array( + 'ID' => $post_id, + 'post_name' => $post_id . '', + ) + ); + } +} + +add_action( 'save_post_wp_navigation', 'gutenberg_set_unique_slug_on_create_wp_navigation' );