Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
demountable
/
wp-content
/
plugins
/
surerank
/
src
/
global
/
hooks
:
use-post-permalink.js
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
import { useState } from '@wordpress/element'; const usePostPermalink = () => { const getCurrentPermalink = () => { if ( ! wp?.data?.select( 'core/editor' ) ) { return ''; } const { getPermalink, isCurrentPostPublished } = wp.data.select( 'core/editor' ); const permalink = getPermalink(); const isPublished = isCurrentPostPublished(); if ( ( isPublished && permalink ) || ( permalink && ! permalink?.includes( 'auto-draft' ) ) ) { return permalink; } return ''; }; const [ postPermalink, setPostPermalink ] = useState( getCurrentPermalink() ); wp.data.subscribe( () => { const permalink = getCurrentPermalink(); if ( ! permalink || permalink === postPermalink ) { return; } setPostPermalink( permalink ); } ); return postPermalink; }; export default usePostPermalink;