Skip to content

Instantly share code, notes, and snippets.

@k1sul1
Created July 11, 2016 09:36
Show Gist options
  • Save k1sul1/f3dbdf98339b9e3d71b5a5171919ae27 to your computer and use it in GitHub Desktop.
Save k1sul1/f3dbdf98339b9e3d71b5a5171919ae27 to your computer and use it in GitHub Desktop.
Get all shortcodes in a chunk of text and replace them in place using these two functions
<?php
class rnb {
function __construct() {
// nothing here.
}
// insert loads of functions here
public static function has_shortcode($text){
if(strpos($text, '[') !== false) {
preg_match_all('/' . get_shortcode_regex() . '/s', $text, $matches, PREG_SET_ORDER);
if (!empty( $matches)){
return $matches;
}
}
return false;
}
public static function do_shortcodes($matches, $text){
// expecting $matches to be a valid output from self::has_shortcode
foreach($matches as $match){
$text = str_replace($match[0], do_shortcode($match[0]), $text);
}
return $text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment