Created
July 11, 2016 09:36
-
-
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
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 | |
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