Notifications
Clear all

wpForo 1.x.x [Solved] Show a picture

9 Posts
7 Users
4 Reactions
2,439 Views
Vít Hofman
Posts: 139
Topic starter
(@vit-hofman)
Estimable Member
Joined: 6 years ago

Hi, I use wpForo with addon embeds. Can I somehow make the post uploaded a photo, it was displayed as a photo and not as a link, see the attached picture on link.

Thank you.

https://photos.app.goo.gl/oqwMhDpN3i2yhRvQ9

Topic Tags
8 Replies
Florian
Posts: 92
(@florian)
Estimable Member
Joined: 6 years ago

You can do that with the attachments addon

1 Reply
Vít Hofman
(@vit-hofman)
Joined: 6 years ago

Estimable Member
Posts: 139

Ah, and I thought it was addon embeds. Thank you.

Posts: 1602
(@anonymous20)
Noble Member
Joined: 9 years ago

Also try this in your theme's functions:

add_filter('wpforo_content_after', 'wpforo_default_attachment_image_embed', 11);
function wpforo_default_attachment_image_embed( $content ){
if( preg_match_all('|<a class=\"wpforo\-default\-attachment\" href\=\"([^\"\']+)\"[^><]*>.+?<\/a>|is', $content, $data, PREG_SET_ORDER) ){
foreach($data as $array){
if(isset($array[1])){
$file = $array[1];
$e = strtolower(substr(strrchr($file, '.'), 1));
if( $e == 'jpg' || $e == 'jpeg' || $e == 'png' || $e == 'gif' ){
$filename = explode('/', $file); $filename = end($filename);
$html = '<a href="' . esc_url($file) . '" target="_blank"><img class="wpforo-default-image-attachment" src="' . esc_url($file) . '" alt="' . esc_attr($filename) . '" title="' . esc_attr($filename) . '" /></a>';
$content = str_replace($array[0], $html, $content);
}
}
}
}
return $content;
}

 

https://wpforo.com/community/how-to-and-troubleshooting-2/uploading-and-embedding-images/#post-5212

1 Reply
(@crisw)
Joined: 6 years ago

Reputable Member
Posts: 281
Posted by: Anonymous20

Also try this in your theme's functions:

add_filter('wpforo_content_after', 'wpforo_default_attachment_image_embed', 11);
function wpforo_default_attachment_image_embed( $content ){
if( preg_match_all('|<a class=\"wpforo\-default\-attachment\" href\=\"([^\"\']+)\"[^><]*>.+?<\/a>|is', $content, $data, PREG_SET_ORDER) ){
foreach($data as $array){
if(isset($array[1])){
$file = $array[1];
$e = strtolower(substr(strrchr($file, '.'), 1));
if( $e == 'jpg' || $e == 'jpeg' || $e == 'png' || $e == 'gif' ){
$filename = explode('/', $file); $filename = end($filename);
$html = '<a href="' . esc_url($file) . '" target="_blank"><img class="wpforo-default-image-attachment" src="' . esc_url($file) . '" alt="' . esc_attr($filename) . '" title="' . esc_attr($filename) . '" /></a>';
$content = str_replace($array[0], $html, $content);
}
}
}
}
return $content;
}

 

https://wpforo.com/community/how-to-and-troubleshooting-2/uploading-and-embedding-images/#post-5212

Hi @anonymous20 . I was about to post that too.  🙂  I also used @Robert's code several months ago (thanks to Robert 🙂 ), as found on wpForo's very robust search box 🙂

Using that code, however, I noticed that if my forum users uploads a picture that is bigger than the width of the forum post, then the image protrudes.  So I added a "tweak" to that code, and I might as well share it here, too.  

I just added this: 

 $html = '<a href="' . esc_url($file) . '" target="_blank"><img class="wpforo-default-image-attachment" src="' . esc_url($file) . '" alt="' . '"max-width="100%"' . esc_attr($filename) . '" title="' . esc_attr($filename) . '" /></a>';

 

Hi @vit-hofman . So my revised entire code that I use is below.  Feel free to use, but use at your own risk 🙂 The code below though, works on my forum, and the effect or result is, if a user uploads a small image, it comes out small, and if a bigger image, then it clips, or resizes to the 100% width of the forum post's width. Just add the code below to your WP THEME's functions.php file: 

// CW-Revised Code to Convert Picture Attachments in wpForo
add_filter('wpforo_content_after', 'wpforo_default_attachment_image_embed', 11);
function wpforo_default_attachment_image_embed( $content ){
if( preg_match_all('|<a class=\"wpforo\-default\-attachment\" href\=\"([^\"\']+)\"[^><]*>.+?<\/a>|is', $content, $data, PREG_SET_ORDER) ){
foreach($data as $array){
if(isset($array[1])){
$file = $array[1];
$e = strtolower(substr(strrchr($file, '.'), 1));
if( $e == 'jpg' || $e == 'jpeg' || $e == 'png' || $e == 'gif' ){
$filename = explode('/', $file); $filename = end($filename);
$html = '<a href="' . esc_url($file) . '" target="_blank"><img class="wpforo-default-image-attachment" src="' . esc_url($file) . '" alt="' . '"max-width="100%"' . esc_attr($filename) . '" title="' . esc_attr($filename) . '" /></a>';
$content = str_replace($array[0], $html, $content);
}
}
}
}
return $content;
}

Hope that helps.  Good luck and God bless you!  🙂 

Posts: 7
 Matt
(@matt)
Active Member
Joined: 3 years ago

I have added that code snippet too, and it worked a treat for showing one image in a post. Thank you!  However, if I go back to edit any post with an image displaying, then I save the edit, the photo then reverts back to being displayed as a link. Any ideas please? Many thanks.

3 Replies
dimalifragis
(@dimalifragis)
Joined: 4 years ago

Famed Member
Posts: 2615

@matt Try this:

 

add_filter('wpforo_content_after', function( $content ){
	return preg_replace_callback(
        '#<a[^><]*\sclass=[\'\"](?:[^\'\"]*\s)?wpforo-default-attachment(?:\s[^\'\"]*)?[\'\"][^><]*\shref=[\'\"]([^\"\']+)[\'\"][^><]*>.*?</a>#isu',
        function( $match ){
                $html     = $match[0];
	        $file     = $match[1];
	        $pathinfo = pathinfo( $file );
	        if( wpforo_is_image($pathinfo['extension']) ) {
                $html = sprintf(
                    '<a href="%1$s" target="_blank"><img class="wpforo-default-image-attachment" src="%1$s" alt="%2$s" title="%2$s"></a>',
	                esc_url($file),
	                esc_attr($pathinfo['basename'])
                );
	        }
            return $html;
        },
        $content
    );
}, 11);
 Matt
(@matt)
Joined: 3 years ago

Active Member
Posts: 7

@dimalifragis

You are an absolute star, thank you! Works perfectly.

(@sydchako)
Joined: 3 years ago

Eminent Member
Posts: 12

@dimalifragis thnks man you solved my problem too