Created
January 30, 2015 16:43
-
-
Save joelworsham/fa17c93d8fb175892b3c to your computer and use it in GitHub Desktop.
Dynamic Shortcode Scripts
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 | |
// Add our action | |
add_action( 'wp_enqueue_scripts', 'joelworsham_add_shortcode_styles' ); | |
/** | |
* Dynamically calls scripts and styles based on the presence | |
* of shortcodes in the post content. | |
*/ | |
function joelworsham_add_shortcode_styles() { | |
global $post, $shortcode_tags; | |
// Bail if the $post object isn't present, | |
// if it has no post content, | |
// or if post content is empty | |
if ( ! is_object( $post ) || | |
! isset( $post->post_content ) || | |
empty( $post->post_content ) | |
) { | |
return; | |
} | |
// Make WP think that syntax highlighter is the only | |
// shortocde when getting the shortcode RegEx (then set it back) | |
$backup_shortcode_tags = $shortcode_tags; | |
$shortcode_tags = array( | |
'syntax_highlighter' => null, | |
); | |
$pattern = get_shortcode_regex(); | |
$shortcode_tags = $backup_shortcode_tags; | |
// Match all existing syntax highlighter shortcodes | |
preg_replace_callback( | |
"/$pattern/s", | |
'_joelworsham_match_codes', | |
$post->post_content | |
); | |
} |
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 | |
/** | |
* Adds our styles and scripts based on the matches. | |
* | |
* @access private | |
* | |
* @param array $matches The found matches. | |
*/ | |
function _joelworsham_match_codes( $matches ) { | |
// "Extract" the attributes | |
$atts = $matches[3]; | |
// Get the attributes into a nicely formatted associative | |
// array, and setup defaults | |
$atts = shortcode_atts( array( | |
'style' => 'default', | |
'language' => 'php' | |
), shortcode_parse_atts( $atts ) ); | |
// If we've found even one syntax highilghter shortcode, | |
// load the dependencies (just once though!) and dynamic | |
// stylesheet | |
static $did_one; | |
if ( $did_one !== true ) { | |
// Stylesheet dependencies (can be whatever you need!) | |
wp_enqueue_style( 'syntax-highlighter-default' ); | |
wp_enqueue_style( 'syntax-highlighter-core' ); | |
// Script dependencies (can also be whateve you need!) | |
wp_enqueue_script( 'syntax-highlighter-core' ); | |
// Dynamic stylesheet | |
wp_enqueue_style( "syntax-highlighter-{$atts['style']}" ); | |
// Make sure this only fires once | |
$did_one = true; | |
} | |
// Enqueue our dynamic scripts | |
static $scripts; | |
if ( ! in_array( $atts['language'], (array) $scripts ) ) { | |
wp_enqueue_script( "syntax-highlighter-{$atts['language']}" ); | |
$scripts[] = $atts['language']; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment