WordPress [img] tags
Hey guys, I'm trying to allow that standard BB [img] [/img] tags on my WordPress blog. I got this snippet from t开发者_StackOverflow社区he net, but it only works on lower case [img] tags. I'd like it to apply to both [img] and [IMG]. As you can tell, I'm totally not a coder.
function embed_images($content) {
$content = preg_replace('/\[img=?\]*(.*?)(\[\/img)?\]/e', '"<img src=\"$1\" alt=\"" . basename("$1") . "\" />"', $content);
return $content;
}
add_filter('comment_text', 'embed_images');
I know '||' is 'or' but don't know enough coding to make the changes. Any help is greatly appreciated. Thanks.
The quick solution would be to make the regex case-insensitive: Replace
'/\[img=?\]*(.*?)(\[\/img)?\]/e'
with
'/\[img=?\]*(.*?)(\[\/img)?\]/ei'
Try:
$content = preg_replace('/\[(img|IMG)=?\]*(.*?)(\[\/(img|IMG))?\]/e', '"<img src=\"$2\" alt=\"" . basename("$2") . "\" />"', $content);
You could also check the Shortcode api of wordpress, which takes care of all the regex and lets you implement this kind of thing easily, including tags with parameters and nested tags.
精彩评论