Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save keefo/faa96292d1282917cdd7a7640245f954 to your computer and use it in GitHub Desktop.
Save keefo/faa96292d1282917cdd7a7640245f954 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Another Syntax Highlighter
* Version: 0.1
* Plugin URI: https://norbertvajda.wordpress.com/
* Description: Just another syntax highligher plugin. It works with prism.js (https://prismjs.com/).
* Author: Norbert Vajda
* Author URI: https://norbertvajda.wordpress.com/
* Network: false
* Text Domain: ash
* Domain Path: /languages
*/
/**
* @author Norbert Vajda
* @copyright Norbert Vajda, 2019, All Rights Reserved
* This code is released under the GPL licence version 3 or later, available here
* https://www.gnu.org/licenses/gpl.txt
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Admin Page
*/
function another_Syntax_Highlighter_setup_menu(){
add_menu_page(
'Another Syntax Highlighter page',
'Another Syntax Highlighter',
'manage_options',
'another-syntax-highlighter',
'ash_admin_init',
'dashicons-editor-code',
66
);
}
function ash_admin_init() {
?>
<div class="wrap">
<h1>Another Syntax Highlighter Settings</h1>
<form action="post" action="options.php">
<?php settings_fields( 'myoption-group' ); ?>
<?php submit_button("Let's go!"); ?>
</form>
</div>
<?php
}
add_action('admin_menu', 'another_Syntax_Highlighter_setup_menu');
/**
* bind prism.js and prism.css, when post / page has this shortcode
*/
function highlighter_enqueue_scripts() {
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'another-syntax-highlighter') ) {
wp_register_style( 'prism-stylesheet', plugin_dir_url( __FILE__ ) . 'assets/css/prism.css' );
wp_register_script( 'prism-script', plugin_dir_url( __FILE__ ) . 'assets/js/prism.js' );
wp_enqueue_style( 'prism-stylesheet' );
wp_enqueue_script( 'prism-script' );
}
}
add_action( 'wp_enqueue_scripts', 'highlighter_enqueue_scripts');
function another_Syntax_Highlighter( $atts, $content = null, $tag = '' ) {
// normalize attribute keys, lowercase
$atts = array_change_key_case( (array)$atts, CASE_LOWER );
extract( shortcode_atts(
array(
'lang' => '',
),
$atts,
'another-syntax-highlighter'
) );
// only the following attributes values
if( in_array( $lang, array( 'markup', 'html', 'xml', 'svg', 'mathml', 'css', 'clike', 'javascript', 'js' ) ) ){
$value = esc_attr($lang);
} else {
$value = 'css';
}
$output = '<pre><code class="language-' . esc_attr($lang) . '">' . $content . '</code></pre>';
return $output;
}
function shortcode_init() {
add_shortcode('another-syntax-highlighter', 'another_Syntax_Highlighter');
}
add_action('init', 'shortcode_init');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment