Last active
August 29, 2015 13:57
-
-
Save hnagata/9466550 to your computer and use it in GitHub Desktop.
excode_filter
This file contains hidden or 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
function excode_filter($content) { | |
return preg_replace_callback('/(\\[?)(\\[excode ([^\\]]*?)\\])\\]?/', function($m) { | |
if ($m[1]) return $m[2]; | |
$out = '[' . 'code'; | |
$pairs = preg_split("/\s+/", $m[3]); | |
foreach ($pairs as $pair) { | |
list($key, $value) = explode('=', $pair); | |
if ($key == 'src') $src_url = preg_replace('/["\']/', '', $value); | |
elseif ($key == 'link') $link_url = preg_replace('/["\']/', '', $value); | |
elseif ($key == 'title') $title = preg_replace('/["\']/', '', $value); | |
else $out .= ' ' . $pair; | |
} | |
$link_url = $link_url ? $link_url : $src_url; | |
$title = $title ? $title : $link_url; | |
$src = $src_url ? file_get_contents($src_url) : FALSE; | |
$out .= sprintf(' title=\'<a href="%s">%s</a>\']', $link_url, $title); | |
$out .= $src !== FALSE ? $src : '(Not available)'; | |
$out .= '[' . '/code]'; | |
return $out; | |
}, $content); | |
} | |
add_filter('the_content', 'excode_filter', 6); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment