diff --git a/packages/fields/src/fields/slug/slug-edit.tsx b/packages/fields/src/fields/slug/slug-edit.tsx
index aeb02ba42873b9..aad6610550069c 100644
--- a/packages/fields/src/fields/slug/slug-edit.tsx
+++ b/packages/fields/src/fields/slug/slug-edit.tsx
@@ -21,6 +21,7 @@ import { __ } from '@wordpress/i18n';
* Internal dependencies
*/
import type { BasePost } from '../../types';
+import { getSlug } from './utils';
const SlugEdit = ( {
field,
@@ -29,7 +30,7 @@ const SlugEdit = ( {
}: DataFormControlProps< BasePost > ) => {
const { id } = field;
- const slug = field.getValue( { item: data } ) ?? '';
+ const slug = field.getValue( { item: data } ) || getSlug( data );
const permalinkTemplate = data.permalink_template || '';
const PERMALINK_POSTNAME_REGEX = /%(?:postname|pagename)%/;
const [ prefix, suffix ] = permalinkTemplate.split(
@@ -115,30 +116,26 @@ const SlugEdit = ( {
}
} }
aria-describedby={ postUrlSlugDescriptionId }
- help={
- <>
-
-
- { __( 'Permalink:' ) }
-
-
-
- { permalinkPrefix }
-
-
- { slugToDisplay }
-
-
- { permalinkSuffix }
-
-
-
- >
- }
/>
+
+
+ { __( 'Permalink:' ) }
+
+
+
+ { permalinkPrefix }
+
+
+ { slugToDisplay }
+
+
+ { permalinkSuffix }
+
+
+
) }
{ ! isEditable && (
diff --git a/packages/fields/src/fields/slug/slug-view.tsx b/packages/fields/src/fields/slug/slug-view.tsx
index dc17eae128d68a..5aa1db048bd413 100644
--- a/packages/fields/src/fields/slug/slug-view.tsx
+++ b/packages/fields/src/fields/slug/slug-view.tsx
@@ -7,9 +7,10 @@ import { useEffect, useRef } from '@wordpress/element';
* Internal dependencies
*/
import type { BasePost } from '../../types';
+import { getSlug } from './utils';
const SlugView = ( { item }: { item: BasePost } ) => {
- const slug = item.slug;
+ const slug = item ? getSlug( item ) : '';
const originalSlugRef = useRef( slug );
useEffect( () => {
@@ -20,7 +21,7 @@ const SlugView = ( { item }: { item: BasePost } ) => {
const slugToDisplay = slug || originalSlugRef.current;
- return `/${ slugToDisplay ?? '' }`;
+ return `${ slugToDisplay ?? '' }`;
};
export default SlugView;
diff --git a/packages/fields/src/fields/slug/utils.ts b/packages/fields/src/fields/slug/utils.ts
new file mode 100644
index 00000000000000..992a21d44ccf65
--- /dev/null
+++ b/packages/fields/src/fields/slug/utils.ts
@@ -0,0 +1,15 @@
+/**
+ * WordPress dependencies
+ */
+import { cleanForSlug } from '@wordpress/url';
+/**
+ * Internal dependencies
+ */
+import type { BasePost } from '../../types';
+import { getItemTitle } from '../../actions/utils';
+
+export const getSlug = ( item: BasePost ) => {
+ return (
+ item.slug || cleanForSlug( getItemTitle( item ) ) || item.id.toString()
+ );
+};