WordPress plugin for highlight.js
<?php # -*- coding: utf-8 -*- | |
/** | |
* Plugin Name: T5 Highlight | |
* Description: Use highlight.js on front end. Mark code with <code><pre class="language-php"><code>your code</code></pre></code> | |
* Version: 31.10.14 | |
* Required: 4.0 | |
* Author: Thomas Scholz | |
* Author URI: http://toscho.de | |
* License: MIT | |
* License URI: http://www.opensource.org/licenses/mit-license.php | |
*/ | |
namespace T5\Frontend\Highlight; | |
/** | |
* Class Highlight | |
* | |
* @version 2014.10.31 | |
* @author toscho | |
*/ | |
class Highlight | |
{ | |
/** | |
* @type string | |
*/ | |
private $highlight_url; | |
/** | |
* @param string $plugin_file Path to main file | |
*/ | |
public function __construct( $plugin_file ) | |
{ | |
$this->highlight_url = plugin_dir_url( $plugin_file ) . 'h/'; | |
} | |
/** | |
* @wp-hook wp_loaded | |
* @return void | |
*/ | |
public function setup() | |
{ | |
$this->register(); | |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) ); | |
add_action( 'wp_print_footer_scripts', array( $this, 'initialize' ), 11 ); | |
} | |
/** | |
* Registers script and stylesheet | |
* | |
* @wp-hook wp_loaded | |
* @return void | |
*/ | |
public function register() | |
{ | |
$js_url = $this->highlight_url . 'highlight.pack.js'; | |
wp_register_script( 'highlight', $js_url, array(), FALSE, TRUE ); | |
$css_url = $this->highlight_url . 'styles/hybrid.css'; | |
wp_register_style( 'highlight', $css_url ); | |
} | |
/** | |
* Enqueues script and stylesheet | |
* | |
* @wp-hook wp_enqueue_scripts | |
* @return void | |
*/ | |
public function enqueue() | |
{ | |
wp_enqueue_script( 'highlight' ); | |
wp_enqueue_style( 'highlight' ); | |
} | |
/** | |
* Initializes the script | |
* | |
* @wp-hook wp_print_footer_scripts | |
* @return void | |
*/ | |
public function initialize() | |
{ | |
?><script>hljs.initHighlightingOnLoad();</script><?php | |
} | |
} | |
is_admin() || add_action( 'wp_loaded', array( new Highlight( __FILE__ ), 'setup' ) ); |
This comment has been minimized.
This comment has been minimized.
Note directory Worked like a charm. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
This doesn't work out of the box, you need to add some steps:
h/
.That's it.