不幸的是,HighlightJS 没有在 WordPress 网站中突出显示我的代码。highlightJS 背景显示,但语法没有突出显示。
这是以下代码:
class WordPress_Code_Highlighter {
public function __construct() {
add_action( 'wp_enqueue_scripts', [ $this, 'register_styles' ], 15 );
add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ] );
}
public function register_styles() {
wp_register_style( 'wordpress-code-highlighter', plugin_dir_url( __FILE__ ) . 'bower_components/highlight.js/src/styles/dracula.css', [ ], '9.3.0' );
wp_enqueue_style( 'wordpress-code-highlighter' );
}
public function register_scripts() {
wp_register_script( 'wordpress-code-highlight', plugin_dir_url( __FILE__ ) . 'bower_components/highlight.js/src/highlight.js', [ ], '9.3.0', true );
wp_enqueue_script( 'wordpress-code-highlight' );
wp_register_script( 'wordpress-code-highlighter', plugin_dir_url( __FILE__ ) . 'resources/assets/js/wordpress-code-highlighter.js', [ ], '1.0', true );
wp_enqueue_script( 'wordpress-code-highlighter' );
}
}
new WordPress_Code_Highlighter();
这是Javascript。
jQuery(document).ready(function () {
jQuery('pre code').each(function (i, block) {
hljs.highlightBlock(block);
});
});