Created
June 1, 2017 08:42
-
-
Save thefuxia/10e64f48d5a685b1de0e2404d0e9a4c3 to your computer and use it in GitHub Desktop.
WordPress plugin for highlight.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ) ); |
Note directory h
not h/
Worked like a charm.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't work out of the box, you need to add some steps:
h/
.That's it.