Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to have such feature that when users post in the comment link to the image (jpg, png etc) in would have [img][/img] around it and have rel="fancybox"to open it in the modal window.

A have such code, but it's not working as expected :(

    add_filter('the_content', 'addrellightbox');
add_filter('comment_text', 'addrellightbox');
function addrellightbox($content) {
       global $post;
       $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
       $replacement = '[img]<a$1href=$2$3.$4$5 rel="lightbox" title="'.$post->post_title.'"$6>[/img]';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;
}

2supertrue Yeah it's good but not enough :)

Users of my blog do not want to know about [img] [/img] constructions - they only want to post image. And they are doing it with just inserting a link. And I want to show this image as it should be - clickable and with img tags.

So... Any solution?

share|improve this question
1  
I think you're confused about what shortcodes are and how they work in WordPress. read the shortcode api page in codex. if I understand what you're trying to accomplish, you want a clickable image, you need to insert an html image tag: <img src="path/to/image" /> and wrap it in an anchor tag. Shortcodes don't do anything if you've not created a shortcode handler to parse it and output actual html markup. – Milo Sep 23 '11 at 7:10

closed as not a real question by toscho Jul 14 '12 at 21:33

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

You'll save a lot of trouble using Wordpress's built-in shortcode functionality. Here's the most basic possible shortcode (borrowed from Aaron), so you can see how it works.

function my_shortcode_function(){
    return date('Y');
}
add_shortcode('year', 'my_shortcode_function');

This would create a shortcode [year] that would display the current year.

To create a shortcode that wraps content like you want to wrap the image URL, you would use the $content parameter. $content gets the text in between the wrapper tags.

function img_shortcode_29252wpse($atts, $content=null){
    return <a href="' . $content . '" rel="lightbox"><img src="' . $content . '" /></a>';
}
add_shortcode('img', 'img_shortcode_29252wpse');

That would replace a code like [img]http://url.of/image.gif[/img] with an image linking to a lightbox version of itself. I'm not 100% from your question if that's what you're going for, but you can use your basic PHP and HTML to adjust the returned code to taste.

It would also be a good idea to wrap the return statement in a conditional like if isset($content) (or, better yet, a basic validation that it is an image URL) so that the shortcode only returns something if there is text inside the [img] and [/img].

share|improve this answer
2supertrue Yeah it's good but not enough :) Users of my blog do not want to know about [img] [/img] constructions - they only want to post image. And they are doing it with just inserting a link. And I want to show this image as it should be - clickable and with img tags. So... Any solution? – Baf Sep 24 '11 at 8:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.