我使用以下命令将特定脚本排入post.php
, 由admin_enqueue_scripts
挂钩:
if ( in_array( get_current_screen()->base, [ \'post\' ] ) ) {
// Load scripts only on the post edit page.
wp_enqueue_script( PLUGIN_NAME, PLUGIN_ROOT_URL . \'resources/js/script.js\', [], PLUGIN_VERSION, false );
}
您可以检索帖子的内容并扫描它以查找所需的块,而不是我所做的检查。像这样:
if ( is_singular() ) {
$post = get_post();
if ( $post && has_blocks( $post ) ) {
$blocks = parse_blocks( $post->post_content );
$postContainsBlock = ... // Search $blocks array for the required block.
if ( $postContainsBlock ) {
wp_enqueue_script( PLUGIN_NAME, PLUGIN_ROOT_URL . \'resources/js/script.js\', [], PLUGIN_VERSION, false );
}
}
}
Note: 我还没有测试上面的代码。