Notifications
Clear all

Script [Solved] Images inline

6 Posts
2 Users
2 Reactions
2,339 Views
dimalifragis
Posts: 2611
Topic starter
(@dimalifragis)
Famed Member
Joined: 5 years ago

Hello.

We use for ages now a small code provided by the support guys, that each attachment that was a photo, was show inline (embeded) to the body. Worked perfectly well.

Now it seems something changed, and if I EDIT such a post, the image becomes attachment after saving.

Any ideas how to fix this?

 

Thanks

5 Replies
dimalifragis
Posts: 2611
Topic starter
(@dimalifragis)
Famed Member
Joined: 5 years ago

The code is:

 

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;
}
1 Reply
Chris
(@chris)
Joined: 3 years ago

Famed Member
Posts: 3627

Hi @dimalifragis,

Delete the old Code and add the new one:

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);
dimalifragis
Posts: 2611
Topic starter
(@dimalifragis)
Famed Member
Joined: 5 years ago

14-Oct-2021 16:53:33 UTC] PHP Warning: preg_replace_callback(): Compilation failed: unrecognized character after (? or (?- at offset 23 in /home/wp-content/plugins/custom-functions/custom-functions.php on line 52

 

Line 52 is

$content
Chris
Posts: 3627
(@chris)
Famed Member
Joined: 3 years ago

@dimalifragis,

Copy and paste the Code again, please. We have updated in my above reply.

dimalifragis
Posts: 2611
Topic starter
(@dimalifragis)
Famed Member
Joined: 5 years ago

Works fine now, many thanks.