本文档详细介绍了 WordPress 核心编辑器(core/editor)数据存储中的选择器和操作,用于管理文章编辑状态、块操作、保存功能等。面向开发者,提供了获取和修改编辑器数据的 API 参考。
// 获取当前文章类型
const currentPostType = wp.data.select( 'core/editor' ).getCurrentPostType();
// 编辑文章标题
wp.data.dispatch( 'core/editor' ).editPost( { title: `${ newTitle }` } );
// 检查自动保存是否锁定
const isAutoSavingLocked = useSelect(
( select ) => select( editorStore ).isPostAutosavingLocked(),
[]
);Namespace: core/editor.
Related
Returns whether or not the user has the unfiltered_html capability.
Parameters
Object: Editor state.Returns
boolean: Whether the user can or can’t post unfiltered HTML.Returns true if a previous post save was attempted but failed, or false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether the post save failed.Returns true if a previous post save was attempted successfully, or false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether the post was saved successfully.Returns the active post lock.
Parameters
Object: Global application state.Returns
Object: The lock object.Related
Deprecated since 5.6. Callers should use the
getAutosave( postType, postId, userId )selector from the ‘@wordpress/core-data’ package and access properties on the returned autosave object using getPostRawValue.
Returns an attribute value of the current autosave revision for a post, or null if there is no autosave for the post.
Parameters
Object: Global application state.string: Autosave attribute name.Returns
*: Autosave attribute value.Related
Related
Related
Related
Related
Related
Related
Related
Related
Related
Related
Related
Related
Related
Related
Related
Related
Returns the post currently being edited in its last known saved state, not including unsaved edits. Returns an object containing relevant default post values if the post has not yet been saved.
Parameters
Object: Global application state.Returns
Object: Post object.Returns an attribute value of the saved post.
Parameters
Object: Global application state.string: Post attribute name.Returns
*: Post attribute value.Returns the ID of the post currently being edited, or null if the post has not yet been saved.
Parameters
Object: Global application state.Returns
?(number|string): The current post ID (number) or template slug (string).Returns the last revision ID of the post currently being edited, or null if the post has no revisions.
Parameters
Object: Global application state.Returns
?number: ID of the last revision.Returns the number of revisions of the post currently being edited.
Parameters
Object: Global application state.Returns
number: Number of revisions.Returns the post type of the post currently being edited.
Usage
const currentPostType = wp.data.select( 'core/editor' ).getCurrentPostType();
Parameters
Object: Global application state.Returns
string: Post type.Returns the template ID currently being rendered/edited
Parameters
Object: Global application state.Returns
?string: Template ID.Returns the current editing canvas device type.
Parameters
Object: Global application state.Returns
string: Device type.Returns a single attribute of the post being edited, preferring the unsaved edit if one exists, but falling back to the attribute for the last known saved state of the post.
Usage
// Get specific media size based on the featured media ID
// Note: change sizes?.large for any registered size
const getFeaturedMediaUrl = useSelect( ( select ) => {
const getFeaturedMediaId =
select( 'core/editor' ).getEditedPostAttribute( 'featured_media' );
const media = select( 'core' ).getEntityRecord(
'postType',
'attachment',
getFeaturedMediaId
);
return (
media?.media_details?.sizes?.large?.source_url ||
media?.source_url ||
''
);
}, [] );
Parameters
Object: Global application state.string: Post attribute name.Returns
*: Post attribute value.Returns the content of the post being edited.
Parameters
Object: Global application state.Returns
string: Post content.Returns the post preview link
Parameters
Object: Global application state.Returns
string | undefined: Preview Link.Returns the slug for the post being edited, preferring a manually edited value if one exists, then a sanitized version of the current post title, and finally the post ID.
Parameters
Object: Editor state.Returns
string: The current slug to be displayed in the editorReturns the current visibility of the post being edited, preferring the unsaved value if different than the saved post. The return value is one of “private”, “password”, or “public”.
Parameters
Object: Global application state.Returns
string: Post visibility.Return the current block list.
Parameters
Object:Returns
Array: Block list.Returns the current editing mode.
Parameters
Object: Global application state.Returns
string: Editing mode.Returns the current selection.
Parameters
Object:Returns
WPBlockSelection: The selection end.Deprecated since Gutenberg 10.0.0.
Returns the current selection end.
Parameters
Object:Returns
WPBlockSelection: The selection end.Deprecated since Gutenberg 10.0.0.
Returns the current selection start.
Parameters
Object:Returns
WPBlockSelection: The selection start.Returns the post editor settings.
Parameters
Object: Editor state.Returns
Object: The editor settings object.Related
Related
Related
Related
Related
Related
Related
Related
Related
Returns the permalink for the post.
Parameters
Object: Editor state.Returns
?string: The permalink, or null if the post is not viewable.Returns the permalink for a post, split into its three parts: the prefix, the postName, and the suffix.
Parameters
Object: Editor state.Returns
Object: An object containing the prefix, postName, and suffix for the permalink, or null if the post is not viewable.Returns any post values which have been changed in the editor but not yet been saved.
Parameters
Object: Global application state.Returns
Object: Object of key value pairs comprising unsaved edits.Returns details about the post lock user.
Parameters
Object: Global application state.Returns
Object: A user object.Returns a post type label depending on the current post.
Parameters
Object: Global application state.Returns
string|undefined: The post type label if available, otherwise undefined.Related
Returns the post editor’s rendering mode.
Parameters
Object: Editor state.Returns
string: Rendering mode.Related
Related
Related
Related
Deprecated since Gutenberg 9.7.0.
Returns state object prior to a specified optimist transaction ID, or null if the transaction corresponding to the given ID cannot be found.
Returns a suggested post format for the current post, inferred only if there is a single block within the post and it is of a type known to match a default post format. Returns null if the format cannot be determined.
Returns
?string: Suggested post format.Related
Related
Returns true if content includes unsaved changes, or false otherwise.
Parameters
Object: Editor state.Returns
boolean: Whether content includes unsaved changes.Returns true if any future editor history snapshots exist, or false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether redo history exists.Returns true if any past editor history snapshots exist, or false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether undo history exists.Related
Related
Returns true if there are unsaved edits for entities other than the editor’s post, and false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether there are edits or not.Related
Related
Deprecated since Gutenberg 9.7.0.
Returns true if an optimistic transaction is pending commit, for which the before state satisfies the given predicate function.
Related
Returns true if the post is autosaving, or false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether the post is autosaving.Related
Related
Related
Related
Related
Related
Returns true if there are no unsaved values for the current edit session and if the currently edited post is new (has never been saved before).
Parameters
Object: Global application state.Returns
boolean: Whether new post and unsaved values exist.Returns whether the collaboration is enabled for the current post.
Returns
boolean: Whether collaboration is enabled.Returns true if post is pending review.
Parameters
Object: Global application state.Returns
boolean: Whether current post is pending review.Return true if the current post has already been published.
Parameters
Object: Global application state.[Object]: Explicit current post for bypassing registry selector.Returns
boolean: Whether the post has been published.Returns true if post is already scheduled.
Parameters
Object: Global application state.Returns
boolean: Whether current post is scheduled to be posted.Returns true if the post is currently being deleted, or false otherwise.
Parameters
Object: Editor state.Returns
boolean: Whether post is being deleted.Returns true if the post can be autosaved, or false otherwise.
Parameters
Object: Global application state.Object: A raw autosave object from the REST API.Returns
boolean: Whether the post can be autosaved.Return true if the post being edited is being scheduled. Preferring the unsaved status values.
Parameters
Object: Global application state.Returns
boolean: Whether the post has been published.Returns whether the current post should be considered to have a “floating” date (i.e. that it would publish “Immediately” rather than at a set time).
Unlike in the PHP backend, the REST API returns a full date string for posts where the 0000-00-00T00:00:00 placeholder is present in the database. To infer that a post is set to publish “Immediately” we check whether the date and modified date are the same.
Parameters
Object: Editor state.Returns
boolean: Whether the edited post has a floating date value.Returns true if there are unsaved values for the current edit session, or false if the editing state matches the saved or new post.
Parameters
Object: Global application state.Returns
boolean: Whether unsaved values exist.Returns true if the edited post has content. A post has content if it has at least one saveable block or otherwise has a non-empty content property assigned.
Parameters
Object: Global application state.Returns
boolean: Whether post has content.Returns true if the currently edited post is yet to be saved, or false if the post has been saved.
Parameters
Object: Global application state.Returns
boolean: Whether the post is new.Return true if the post being edited can be published.
Parameters
Object: Global application state.Returns
boolean: Whether the post can been published.Returns true if the post can be saved, or false otherwise. A post must contain a title, an excerpt, or non-empty content to be valid for save.
Parameters
Object: Global application state.Returns
boolean: Whether the post can be saved.Returns true if the given panel is enabled, or false otherwise. Panels are enabled by default.
Parameters
Object: Global application state.string: A string that identifies the panel.Returns
boolean: Whether or not the panel is enabled.Returns true if the given panel is open, or false otherwise. Panels are closed by default.
Parameters
Object: Global application state.string: A string that identifies the panel.Returns
boolean: Whether or not the panel is open.Returns true if the given panel was programmatically removed, or false otherwise. All panels are not removed by default.
Parameters
Object: Global application state.string: A string that identifies the panel.Returns
boolean: Whether or not the panel is removed.Related
Returns true if the inserter is opened.
Parameters
Object: Global application state.Returns
boolean: Whether the inserter is opened.Returns true if the list view is opened.
Parameters
Object: Global application state.Returns
boolean: Whether the list view is opened.Related
Returns whether the permalink is editable or not.
Parameters
Object: Editor state.Returns
boolean: Whether or not the permalink is editable.Returns whether post autosaving is locked.
Usage
import { __ } from '@wordpress/i18n';
import { store as editorStore } from '@wordpress/editor';
import { useSelect } from '@wordpress/data';
const ExampleComponent = () => {
const isAutoSavingLocked = useSelect(
( select ) => select( editorStore ).isPostAutosavingLocked(),
[]
);
return isAutoSavingLocked ? (
<p>{ __( 'Post auto saving is locked' ) }</p>
) : (
<p>{ __( 'Post auto saving is not locked' ) }</p>
);
};
Parameters
Object: Global application state.Returns
boolean: Is locked.Returns whether the post is locked.
Parameters
Object: Global application state.Returns
boolean: Is locked.Returns whether the edition of the post has been taken over.
Parameters
Object: Global application state.Returns
boolean: Is post lock takeover.Returns whether post saving is locked.
Usage
import { __ } from '@wordpress/i18n';
import { store as editorStore } from '@wordpress/editor';
import { useSelect } from '@wordpress/data';
const ExampleComponent = () => {
const isSavingLocked = useSelect(
( select ) => select( editorStore ).isPostSavingLocked(),
[]
);
return isSavingLocked ? (
<p>{ __( 'Post saving is locked' ) }</p>
) : (
<p>{ __( 'Post saving is not locked' ) }</p>
);
};
Parameters
Object: Global application state.Returns
boolean: Is locked.Returns true if the post is being previewed, or false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether the post is being previewed.Returns true if the post is being published, or false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether post is being published.Returns whether the pre-publish panel should be shown or skipped when the user clicks the “publish” button.
Returns
boolean: Whether the pre-publish panel should be shown or not.Returns true if the publish sidebar is opened.
Parameters
Object: Global application stateReturns
boolean: Whether the publish sidebar is open.Returns true if non-post entities are currently being saved, or false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether non-post entities are being saved.Returns true if the post is currently being saved, or false otherwise.
Parameters
Object: Global application state.Returns
boolean: Whether post is being saved.Related
Related
Related
Action that autosaves the current post. This includes server-side autosaving (default) and client-side (a.k.a. local) autosaving (e.g. on the Web, the post might be committed to Session Storage).
Parameters
[Object]: Extra flags to identify the autosave.[boolean]: Whether to perform a local autosave.Related
Returns an action object used in signalling that the user closed the publish sidebar.
Returns
Object: Action object.Deprecated Since WordPress 6.0
Action that creates an undo history record.
Disables the publish sidebar.
Returns an action object used in signalling that attributes of the post have been edited.
Usage
// Update the post title
wp.data.dispatch( 'core/editor' ).editPost( { title: `${ newTitle }` } );
Parameters
Object: Post attributes to edit.[Object]: Options for the edit.Returns
Object: Action objectEnable the publish sidebar.
Related
Related
Related
Related
Related
Related
Action that locks post autosaving.
Usage
// Lock post autosaving with the lock key `mylock`:
wp.data.dispatch( 'core/editor' ).lockPostAutosaving( 'mylock' );
Parameters
string: The lock name.Returns
Object: Action objectAction that locks post saving.
Usage
const { subscribe } = wp.data;
const initialPostStatus = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'status' );
// Only allow publishing posts that are set to a future date.
if ( 'publish' !== initialPostStatus ) {
// Track locking.
let locked = false;
// Watch for the publish event.
let unssubscribe = subscribe( () => {
const currentPostStatus = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'status' );
if ( 'publish' !== currentPostStatus ) {
// Compare the post date to the current date, lock the post if the date isn't in the future.
const postDate = new Date( wp.data.select( 'core/editor' ).getEditedPostAttribute( 'date' ) );
const currentDate = new Date();
if ( postDate.getTime() <= currentDate.getTime() ) {
if ( ! locked ) {
locked = true;
wp.data.dispatch( 'core/editor' ).lockPostSaving( 'futurelock' );
}
} else {
if ( locked ) {
locked = false;
wp.data.dispatch( 'core/editor' ).unlockPostSaving( 'futurelock' );
}
}
}
} );
}
Parameters
string: The lock name.Returns
Object: Action objectRelated
Related
Related
Related
Related
Returns an action object used in signalling that the user opened the publish sidebar.
Returns
Object: Action objectRelated
Action that restores last popped state in undo history.
Deprecated Since WordPress 6.0.
Action for refreshing the current post.
Related
Related
Returns an action object used to remove a panel from the editor.
Parameters
string: A string that identifies the panel to remove.Returns
Object: Action object.Related
Related
Related
Returns an action object used to signal that the blocks have been updated.
Parameters
Array: Block Array.[Object]: Optional options.Deprecated Since WordPress 6.0.
Returns an action object used in signalling that the latest version of the post has been received, either by initialization or save.
Action for saving the current post in the editor.
Parameters
[Object]:Related
Action that changes the width of the editing canvas.
Parameters
string:Returns
Object: Action object.Returns an action that sets the current post Type and post ID.
Parameters
string: Post Type.string: Post ID.Returns
Object: Action object.Returns an action object used to open/close the inserter.
Parameters
boolean|Object: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object.string: The root client ID to insert at.number: The index to insert at.string: A query to filter the inserter results.Function: A callback when an item is selected.string: The tab to open in the inserter.string: The category to initialize in the inserter.Returns
Object: Action object.Returns an action object used to open/close the list view.
Parameters
boolean: A boolean representing whether the list view should be opened or closed.Returns
Object: Action object.Returns an action used to set the rendering mode of the post editor. We support multiple rendering modes:
post-only: This mode extracts the post blocks from the template and renders only those. The idea is to allow the user to edit the post/page in isolation without the wrapping template.template-locked: This mode renders both the template and the post blocks but the template blocks are locked and can’t be edited. The post blocks are editable.Parameters
string: Mode (one of ‘post-only’ or ‘template-locked’).Related
Returns an action generator used in signalling that editor has initialized with the specified post object and editor settings.
Parameters
Object: Post object.Object: Initial edited attributes object.[Array]: Block Template.Deprecated
Setup the editor state.
Parameters
Object: Post object.Related
Related
Related
Related
Related
Triggers an action used to switch editor mode.
Parameters
string: The editor mode.Related
Related
Action that toggles Distraction free mode. Distraction free mode expects there are no sidebars, as due to the z-index values set, you can’t close sidebars.
Parameters
[Object]: Optional configuration object[boolean]: Whether to create a noticeReturns an action object used to enable or disable a panel in the editor.
Parameters
string: A string that identifies the panel to enable or disable.Returns
Object: Action object.Opens a closed panel and closes an open panel.
Parameters
string: A string that identifies the panel to open or close.Returns an action object used in signalling that the user toggles the publish sidebar.
Returns
Object: Action objectRelated
Action that toggles the Spotlight Mode view option.
Action that toggles the Top Toolbar view option.
Action for trashing the current post in the editor.
Action that pops a record from undo history and undoes the edit.
Action that unlocks post autosaving.
Usage
// Unlock post saving with the lock key `mylock`:
wp.data.dispatch( 'core/editor' ).unlockPostAutosaving( 'mylock' );
Parameters
string: The lock name.Returns
Object: Action objectAction that unlocks post saving.
Usage
// Unlock post saving with the lock key `mylock`:
wp.data.dispatch( 'core/editor' ).unlockPostSaving( 'mylock' );
Parameters
string: The lock name.Returns
Object: Action objectRelated
Related
Related
Undocumented declaration.
Deprecated since Gutenberg 9.7.0.
Returns an action object used in signalling that a patch of updates for the latest version of the post have been received.
Returns
Object: Action object.Action that locks the editor.
Parameters
Object: Details about the post lock status, user, and nonce.Returns
Object: Action object.