user579343
user579343

Reputation:

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 the 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.

Upvotes: 0

Views: 310

Answers (3)

jlasarte
jlasarte

Reputation: 643

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.

Upvotes: 0

scragz
scragz

Reputation: 6690

Try:

$content = preg_replace('/\[(img|IMG)=?\]*(.*?)(\[\/(img|IMG))?\]/e', '"<img src=\"$2\" alt=\"" . basename("$2") . "\" />"', $content);

Upvotes: 0

Anon.
Anon.

Reputation: 59963

The quick solution would be to make the regex case-insensitive: Replace

'/\[img=?\]*(.*?)(\[\/img)?\]/e'

with

'/\[img=?\]*(.*?)(\[\/img)?\]/ei'

Upvotes: 1

Related Questions