Skip to content

Instantly share code, notes, and snippets.

@hnagata
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnagata/9466550 to your computer and use it in GitHub Desktop.
Save hnagata/9466550 to your computer and use it in GitHub Desktop.
excode_filter
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