isSavingPost() for widgets
I want to get the block ID of my widgets (e.g. #block-1
, which is displayed on the frontend after saving a widget) on the widgets screen in the backend. What I try to achieve is to display the ID of the widget in the toolbar so that you can easily copy it.
I currently use a custom API endpoint to receive the ID but it will trigger a request on every render (I already debounced it, but am searching for a better way).
My idea was than to subscribe to the save action of the widgets, since the ID is first available after saving the widgets screen:
const widgetIdToolbar = createHigherOrderComponent( ( BlockEdit ) = {
if ( select( 'core/editor' ).getCurrentPostType() ) {
return BlockEdit;
}
return ( props ) = {
if ( props.name === 'core/widget-area' || props.name === 'core/legacy-widget' ) {
return ( BlockEdit { ...props } / );
}
const [ widgetId, setWidgetId ] = useState( 0 );
subscribe( () = {
const isAutosavingPost = select( 'core/editor' ).isAutosavingPost();
const isSavingPost = select( 'core/editor' ).isSavingPost();
if ( isSavingPost || isAutosavingPost ) {
// do stuff
}
} );
return (
BlockEdit { ...props } /
/
);
}
}, 'widgetIdToolbar' );
addFilter( 'editor.BlockEdit', 'rh-widget-toolbar/add-toolbar', widgetIdToolbar );
Unfortunately, both isAutosavingPost
and isSavingPost
always return false
. It seems they don’t apply to widgets. How to get the similar functionality for “on widgets save”?
Topic block-editor autosave widgets Wordpress
Category Web