Pre-Publish Checklist in the Block Editor
Adds a checklist to the block editor's publish panel, such as "Spell-checked" or "Links tested", and keeps the Publish button locked until every item is ticked.
Opens WPIDE on
Code
// Checklist items shown before publishing.
$wpide_checklist_items = array(
'Spelling and grammar checked',
'Links tested',
'Featured image and alt text set',
'Category and tags chosen',
);
// Post types that get the checklist.
$wpide_checklist_types = array( 'post' );
add_action( 'enqueue_block_editor_assets', function () use ( $wpide_checklist_items, $wpide_checklist_types ) {
$screen = get_current_screen();
if ( ! $screen || 'post' !== $screen->base || ! in_array( $screen->post_type, $wpide_checklist_types, true ) ) {
return;
}
wp_register_script( 'wpide-prepublish-checklist', false, array( 'wp-plugins', 'wp-element', 'wp-components', 'wp-data', 'wp-editor' ), '1.0', true );
wp_enqueue_script( 'wpide-prepublish-checklist' );
wp_add_inline_script( 'wpide-prepublish-checklist', 'var wpideChecklistItems = ' . wp_json_encode( array_values( $wpide_checklist_items ) ) . ';' . <<<'JS'
( function ( wp ) {
var el = wp.element.createElement;
var Panel = ( wp.editor && wp.editor.PluginPrePublishPanel ) || ( wp.editPost && wp.editPost.PluginPrePublishPanel );
if ( ! Panel ) { return; }
function Checklist() {
var state = wp.element.useState( {} );
var checked = state[0], setChecked = state[1];
var done = wpideChecklistItems.every( function ( item, i ) { return checked[ i ]; } );
var isPublished = wp.data.useSelect( function ( select ) { return select( 'core/editor' ).isCurrentPostPublished(); }, [] );
wp.element.useEffect( function () {
var editor = wp.data.dispatch( 'core/editor' );
if ( done || isPublished ) { editor.unlockPostSaving( 'wpide-checklist' ); } else { editor.lockPostSaving( 'wpide-checklist' ); }
return function () { editor.unlockPostSaving( 'wpide-checklist' ); };
}, [ done, isPublished ] );
return el( Panel, { title: 'Checklist', initialOpen: true },
wpideChecklistItems.map( function ( item, i ) {
return el( wp.components.CheckboxControl, {
key: i,
label: item,
checked: !! checked[ i ],
onChange: function ( value ) { var next = Object.assign( {}, checked ); next[ i ] = value; setChecked( next ); }
} );
} ),
done ? null : el( 'p', { style: { color: '#b32d2e' } }, 'Tick every item to enable publishing.' )
);
}
wp.plugins.registerPlugin( 'wpide-prepublish-checklist', { render: Checklist } );
} )( window.wp );
JS
);
} );
Notes
Edit the items in $wpide_checklist_items; ticks are not saved, so the list resets each time the post is published.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.