Skip to content

Commit

Permalink
feat: Add edit post or page button to open edit page of post or page
Browse files Browse the repository at this point in the history
  • Loading branch information
dhananjaykuber committed Jan 23, 2025
1 parent 0d35e9f commit 2e1b986
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ import {
} from '@wordpress/components';
import { useCopyToClipboard } from '@wordpress/compose';
import { filterURLForDisplay, safeDecodeURI } from '@wordpress/url';
import { Icon, globe, info, linkOff, edit, copySmall } from '@wordpress/icons';
import {
Icon,
globe,
info,
linkOff,
edit,
copySmall,
commentEditLink,
} from '@wordpress/icons';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';
Expand Down Expand Up @@ -94,6 +102,25 @@ export default function LinkPreview( {
} );
} );

// Get the edit URL for the post.
const getEditUrl = () => {
const postId = value?.id;
if ( ! postId ) {
return;
}

const url = `/wp-admin/post.php?post=${ postId }&action=edit`;
return url;
};

// Handle the edit post button click.
const handleEditPost = () => {
const editUrl = getEditUrl();
if ( editUrl ) {
window.location.href = editUrl;
}
};

return (
<div
role="group"
Expand Down Expand Up @@ -177,6 +204,13 @@ export default function LinkPreview( {
size="compact"
showTooltip={ ! showIconLabels }
/>
<Button
icon={ commentEditLink }
label={ __( 'Edit Post or Page' ) }
size="compact"
showTooltip={ ! showIconLabels }
onClick={ handleEditPost }
/>
<ViewerSlot fillProps={ value } />
</div>
</div>
Expand Down

0 comments on commit 2e1b986

Please sign in to comment.