{"id":46952,"date":"2026-11-11T09:08:18","date_gmt":"2026-11-11T09:08:18","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46952"},"modified":"2026-11-11T09:08:18","modified_gmt":"2026-11-11T09:08:18","slug":"wordpress-lock-publish-button-allow-save-draft","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wordpress-lock-publish-button-allow-save-draft\/","title":{"rendered":"Lock the Publish Button but Allow Save Draft in WordPress"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the WordPress block editor, a common request is to lock the Publish button but still allow Save Draft. For example, a post shouldn&#8217;t go live without a featured image, but writers still need to save their drafts. The obvious tool, <code>lockPostSaving()<\/code> from the <code>core\/editor<\/code> data store, can&#8217;t do this. It locks every save, so Save Draft stops working too and people lose their work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The snippet below locks only publishing. While the rule isn&#8217;t met, the Publish button is dimmed and a warning notice explains why. A client-side <code>editor.preSavePost<\/code> filter stops a publish attempt before the request is sent. A REST API guard rejects the same request on the server, so the rule still holds when someone uses a keyboard shortcut, a different client or an old editor version. Save Draft, Save as Pending and updates to posts that are already published work as before.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Put it in a small plugin, an mu-plugin or your theme&#8217;s functions.php. It is one self-contained PHP file with no build step: the editor script is printed inline and uses <code>wp.data<\/code>, <code>wp.hooks<\/code> and <code>wp.notices<\/code> from core.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Plugin Name: DPC Publish Lock (Save Draft still allowed)\n * Description: Blocks publishing until the post has a featured image. Drafts can always be saved.\n *\/\n\n\/** Post types the lock applies to. *\/\nfunction dpc_publish_lock_post_types(): array {\n\treturn (array) apply_filters( 'dpc_publish_lock_post_types', array( 'post' ) );\n}\n\n\/** Message shown in the editor and returned by the REST API. *\/\nfunction dpc_publish_lock_message(): string {\n\treturn __( 'Set a featured image before publishing. You can still save this post as a draft.', 'dpc' );\n}\n\n\/**\n * Server-side guard: reject REST requests that would publish or schedule\n * a post that has no featured image yet.\n *\/\nadd_action( 'rest_api_init', 'dpc_publish_lock_register_guards' );\nfunction dpc_publish_lock_register_guards(): void {\n\tforeach ( dpc_publish_lock_post_types() as $post_type ) {\n\t\tadd_filter( \"rest_pre_insert_{$post_type}\", 'dpc_publish_lock_rest_guard', 10, 2 );\n\t}\n}\n\nfunction dpc_publish_lock_rest_guard( $prepared_post, WP_REST_Request $request ) {\n\tif ( is_wp_error( $prepared_post ) ) {\n\t\treturn $prepared_post;\n\t}\n\n\t$live       = array( 'publish', 'future' );\n\t$new_status = $request-&gt;has_param( 'status' ) ? (string) $request&#91;'status'&#93; : '';\n\n\t\/\/ Drafts, pending and requests that don't change the status are always allowed.\n\tif ( ! in_array( $new_status, $live, true ) ) {\n\t\treturn $prepared_post;\n\t}\n\n\t\/\/ Updating a post that is already live is not \"publishing\".\n\t$post_id = isset( $prepared_post-&gt;ID ) ? (int) $prepared_post-&gt;ID : 0;\n\tif ( $post_id &amp;&amp; in_array( get_post_status( $post_id ), $live, true ) ) {\n\t\treturn $prepared_post;\n\t}\n\n\t\/\/ The rule: use the featured image sent with this request, else the stored one.\n\t$thumbnail_id = $request-&gt;has_param( 'featured_media' )\n\t\t? (int) $request&#91;'featured_media'&#93;\n\t\t: ( $post_id ? (int) get_post_thumbnail_id( $post_id ) : 0 );\n\n\tif ( $thumbnail_id &gt; 0 ) {\n\t\treturn $prepared_post;\n\t}\n\n\treturn new WP_Error( 'dpc_publish_locked', dpc_publish_lock_message(), array( 'status' =&gt; 400 ) );\n}\n\n\/** Editor script: dims Publish, shows a notice, stops the publish request. *\/\nfunction dpc_publish_lock_js(): string {\n\treturn &lt;&lt;&lt;'JS'\n( function ( wp, config ) {\n\tif ( ! wp || ! wp.data || ! wp.hooks || ! config ) {\n\t\treturn;\n\t}\n\tconst { select, dispatch, subscribe } = wp.data;\n\tconst LIVE = &#91; 'publish', 'future' &#93;;\n\tconst NOTICE_ID = 'dpc-publish-lock';\n\tconst editor = () =&gt; select( 'core\/editor' );\n\n\t\/\/ The rule. Keep it in sync with dpc_publish_lock_rest_guard().\n\tconst isMissingRequirement = () =&gt;\n\t\t! editor().getEditedPostAttribute( 'featured_media' );\n\n\tconst appliesHere = () =&gt;\n\t\tconfig.postTypes.includes( editor().getCurrentPostType() ) &amp;&amp;\n\t\t! LIVE.includes( editor().getCurrentPost().status );\n\n\t\/\/ Toggle the visual lock and the notice only when the state changes.\n\tlet wasLocked = null;\n\tsubscribe( () =&gt; {\n\t\tif ( ! editor().getCurrentPostId() ) {\n\t\t\treturn;\n\t\t}\n\t\tconst locked = appliesHere() &amp;&amp; isMissingRequirement();\n\t\tif ( locked === wasLocked ) {\n\t\t\treturn;\n\t\t}\n\t\twasLocked = locked;\n\t\tdocument.body.classList.toggle( 'dpc-publish-locked', locked );\n\t\tif ( locked ) {\n\t\t\tdispatch( 'core\/notices' ).createWarningNotice( config.message, {\n\t\t\t\tid: NOTICE_ID,\n\t\t\t\tisDismissible: false,\n\t\t\t} );\n\t\t} else {\n\t\t\tdispatch( 'core\/notices' ).removeNotice( NOTICE_ID );\n\t\t}\n\t} );\n\n\t\/\/ Stop a publish\/schedule save before the request is sent.\n\twp.hooks.addFilter( 'editor.preSavePost', 'dpc\/publish-lock', ( edits, options ) =&gt; {\n\t\tif ( options?.isAutosave || ! LIVE.includes( edits.status ) ) {\n\t\t\treturn edits;\n\t\t}\n\t\tif ( ! appliesHere() || ! isMissingRequirement() ) {\n\t\t\treturn edits;\n\t\t}\n\t\t\/\/ Put the status back so the next Save Draft really saves a draft.\n\t\tconst saved = editor().getCurrentPost().status;\n\t\tdispatch( 'core\/editor' ).editPost( {\n\t\t\tstatus: 'auto-draft' === saved ? 'draft' : saved,\n\t\t} );\n\t\tthrow new Error( config.message );\n\t} );\n} )( window.wp, window.dpcPublishLock );\nJS;\n}\n\nadd_action( 'enqueue_block_editor_assets', 'dpc_publish_lock_editor_assets' );\nfunction dpc_publish_lock_editor_assets(): void {\n\t\/\/ Post editor only: the Site Editor and widget screens have no global post.\n\t$post_type = get_post_type();\n\tif ( ! $post_type || ! in_array( $post_type, dpc_publish_lock_post_types(), true ) ) {\n\t\treturn;\n\t}\n\n\t$config = array(\n\t\t'postTypes' =&gt; array_values( dpc_publish_lock_post_types() ),\n\t\t'message'   =&gt; dpc_publish_lock_message(),\n\t);\n\n\twp_register_script(\n\t\t'dpc-publish-lock',\n\t\tfalse,\n\t\tarray( 'wp-data', 'wp-hooks', 'wp-notices', 'wp-editor' ),\n\t\tnull,\n\t\ttrue\n\t);\n\twp_enqueue_script( 'dpc-publish-lock' );\n\twp_add_inline_script(\n\t\t'dpc-publish-lock',\n\t\t'window.dpcPublishLock = ' . wp_json_encode( $config ) . \";\\n\" . dpc_publish_lock_js()\n\t);\n\n\t$css = 'body.dpc-publish-locked .editor-post-publish-panel__toggle,'\n\t\t. 'body.dpc-publish-locked .editor-post-publish-button{opacity:.5;pointer-events:none;}';\n\twp_register_style( 'dpc-publish-lock', false );\n\twp_enqueue_style( 'dpc-publish-lock' );\n\twp_add_inline_style( 'dpc-publish-lock', $css );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>wp.data.subscribe()<\/code><\/strong> runs whenever an editor store changes. It checks the edited <code>featured_media<\/code> attribute and toggles a <code>dpc-publish-locked<\/code> class on <code>body<\/code>, plus a warning notice that can&#8217;t be dismissed. It only touches the DOM when the locked state changes, so it stays cheap even though it runs on every store update.<\/li>\n\n\n<li><strong><code>editor.preSavePost<\/code><\/strong> is an async filter that <code>savePost()<\/code> runs before it sends the request (WordPress 6.7+). Clicking Publish sets <code>status: 'publish'<\/code> (or <code>'future'<\/code> for a scheduled post) and then saves. The filter sees that status, puts the old status back with <code>editPost()<\/code> and throws. The editor aborts the save and shows &#8220;Publishing failed.&#8221; followed by your message.<\/li>\n\n\n<li><strong><code>rest_pre_insert_{post_type}<\/code><\/strong> is the real lock. The block editor saves through the REST API, so returning a <code>WP_Error<\/code> here rejects any publish or schedule request for a post without a featured image, whatever client sent it. Requests that don&#8217;t set a live status pass straight through, and so does any post that is already published or scheduled.<\/li>\n\n\n<li>The config goes to the script through <code>wp_json_encode()<\/code>, which escapes slashes, so a translated message can&#8217;t break out of the inline <code>&lt;script&gt;<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This differs from <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/block-publish-without-category-and-subcategory\/\">blocking publish until a category and subcategory are set<\/a>: that approach locks saving entirely, while this one keeps drafts saveable. Use whichever fits your writers&#8217; workflow.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>More post types:<\/strong> <code>add_filter( 'dpc_publish_lock_post_types', fn() =&gt; array( 'post', 'page', 'product' ) );<\/code> Every type you add needs <code>thumbnail<\/code> support if you keep the featured-image rule.<\/li>\n\n\n<li><strong>A different rule:<\/strong> change <code>isMissingRequirement()<\/code> in the JS and the <code>$thumbnail_id<\/code> check in PHP together. For a required excerpt, use <code>! editor().getEditedPostAttribute( 'excerpt' ).trim()<\/code> in JS and <code>$request&#91;'excerpt'&#93; ?? get_post_field( 'post_excerpt', $post_id )<\/code> in PHP.<\/li>\n\n\n<li><strong>Let editors bypass it:<\/strong> return <code>$prepared_post<\/code> early in the guard when <code>current_user_can( 'edit_others_posts' )<\/code> is true, and return early from <code>dpc_publish_lock_editor_assets()<\/code> with the same check.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The dimmed button relies on core CSS class names, which aren&#8217;t a public API. If a future release renames them, the button looks clickable again, but <code>editor.preSavePost<\/code> and the REST guard still block publishing.<\/li>\n\n\n<li>Before WordPress 6.7 there is no <code>editor.preSavePost<\/code>. The server guard still rejects the request and the editor shows its usual &#8220;Publishing failed&#8221; notice.<\/li>\n\n\n<li>Quick Edit, bulk edit, XML-RPC and <code>wp_insert_post()<\/code> don&#8217;t go through the REST API. If those paths matter to you, add a <code>wp_insert_post_data<\/code> filter that resets <code>post_status<\/code> to <code>draft<\/code>.<\/li>\n\n\n<li>If you load the snippet with Scripts Organizer instead of functions.php, paste it into one PHP block set to run everywhere. The REST guard has to load on REST requests, not only in the admin.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Lock the Publish button in WordPress until a featured image is set, while Save Draft keeps working. Enforced in the editor and the REST API.<\/p>\n","protected":false},"author":1,"featured_media":46953,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Lock the Publish Button but Allow Save Draft in WordPress","description":"Lock the Publish button in WordPress until a featured image is set, while Save Draft keeps working. Enforced in the editor and the REST API."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[136,3],"tags":[159,244,218,161,243],"dpc_library":[],"class_list":["post-46952","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react","category-wordpress","tag-block-editor","tag-editor-presavepost","tag-react","tag-rest-api","tag-wordpress-data"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46952"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46952\/revisions"}],"predecessor-version":[{"id":46954,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46952\/revisions\/46954"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46953"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46952"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46952"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}