Highlight.js Syntax Highlighter
, (*1)
A simple Highlight.js-based syntax highlighter plugin for WordPress., (*2)
Installation
Install with Composer:, (*3)
composer require tfrommen/highlightjs
Or:, (*4)
-
Download ZIP.
- Upload contents to the
/wp-content/plugins/
directory on your web server.
- Activate the plugin through the Plugins menu in WordPress.
- See syntax highlighting for all code snippets wrapped in
<pre><code>
tags (e.g., a core/code
block).
Requirements
This plugin requires PHP 7.4 or higher., (*5)
Usage
Please refer to the Highlight.js documentation., (*6)
Filters
In order to customize certain aspects of the plugin, it provides you with several filters.
For each of these, a short description as well as a code example on how to alter the default behavior is given below.
Just put the according code snippet in your theme's functions.php
file or your customization plugin, or to some other appropriate place., (*7)
\tfrommen\HighlightJs\FILTER_SHOULD_LOAD
(highlightjs.should_load
)
This filter lets you customize the condition for the plugin to load.
The default value is the result of is_singular( 'post' ) && has_block( 'code' )
, meaning the plugin only loads for single posts that include at least one core/code
block., (*8)
If you want to load the plugin for all single posts, no matter what blocks are included in the content:, (*9)
<?php
/**
* Filters the condition for the plugin to load.
*
* @param bool $should_load Whether or not the plugin should load.
*/
add_filter( \tfrommen\HighlightJs\FILTER_SHOULD_LOAD, function () {
return is_singlular( 'post' );
} );
If you want to load the plugin for all requests, you can use the __return_true
WordPress function:, (*10)
<?php
/**
* Filters the condition for the plugin to load.
*
* @param bool $should_load Whether or not the plugin should load.
*/
add_filter( \tfrommen\HighlightJs\FILTER_SHOULD_LOAD, '__return_true' );
License
Copyright (c) 2017 Thorsten Frommen, (*11)
This code is licensed under the MIT License., (*12)
The included Highlight.js is released under the BSD License., (*13)