You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为WordPress发布前侧边栏的检查列表添加自定义检查项

给WordPress原生发布前检查清单加自定义项

不用装插件,靠WordPress原生工具就能给默认的发布前检查清单加新项,核心是用Gutenberg编辑器的JS过滤器和WP的动作钩子,具体操作如下:

方法1:主题文件+独立JS脚本

第一步:注册加载自定义JS脚本

在当前主题的functions.php里加这段PHP代码,用来注册并加载我们的自定义JS脚本,让它在块编辑器里生效:

add_action( 'enqueue_block_editor_assets', 'custom_prepublish_checklist_script' );
function custom_prepublish_checklist_script() {
    wp_enqueue_script(
        'custom-prepublish-checklist',
        get_stylesheet_directory_uri() . '/js/custom-prepublish-checklist.js',
        array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components' ),
        filemtime( get_stylesheet_directory() . '/js/custom-prepublish-checklist.js' ),
        true
    );
}

第二步:写检查项的JS逻辑

在主题目录下建个js文件夹,里面新建custom-prepublish-checklist.js文件,把下面的代码放进去(示例加了两个检查项,你可以自己改):

wp.hooks.addFilter(
    'editor.PostPublishPanel.prePublishChecks',
    'custom/prepublish-checklist',
    ( checks ) => {
        // 检查是否设置了特色图片
        checks.push( {
            id: 'has-featured-image',
            title: '已设置特色图片',
            isComplete: () => {
                const featuredImageId = wp.data.select('core/editor').getEditedPostAttribute('featured_media');
                return featuredImageId !== 0;
            },
        } );

        // 检查文章内容长度是否够300字
        checks.push( {
            id: 'content-length',
            title: '文章内容不少于300字',
            isComplete: () => {
                const content = wp.data.select('core/editor').getEditedPostAttribute('content');
                const strippedContent = content.replace(/<[^>]+>/g, '').trim();
                return strippedContent.length >= 300;
            },
        } );

        return checks;
    }
);

这段代码靠editor.PostPublishPanel.prePublishChecks过滤器,把新的检查项塞进默认的检查数组里。每个检查项要有唯一ID、显示标题,还有判断是否完成的逻辑——返回true就是通过,false就是没完成。

方法2:直接内联JS代码(简化版)

不想单独建JS文件的话,直接把JS代码嵌到functions.php里也行:

add_action( 'admin_footer-post.php', 'inline_custom_prepublish_checklist' );
add_action( 'admin_footer-post-new.php', 'inline_custom_prepublish_checklist' );
function inline_custom_prepublish_checklist() {
    ?>
    <script>
        wp.hooks.addFilter(
            'editor.PostPublishPanel.prePublishChecks',
            'custom/prepublish-checklist',
            ( checks ) => {
                // 检查是否添加了至少一个分类
                checks.push( {
                    id: 'custom-check-term',
                    title: '已为文章添加至少一个分类',
                    isComplete: () => {
                        const terms = wp.data.select('core/editor').getEditedPostAttribute('categories');
                        return terms.length > 0;
                    },
                } );
                return checks;
            }
        );
    </script>
    <?php
}

额外说明

  • 你可以根据需求改isComplete里的逻辑,比如检查标签、自定义字段、摘要有没有填,都行
  • 上面的代码适配的是Gutenberg块编辑器(WP 5.0及以上版本),如果用的是经典编辑器,得换publish_box动作钩子来加检查项

内容的提问来源于stack exchange,提问作者Rodrigo Alves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 22:40:31