I have added some code into my function to allow me to show images if the link is a direct link to an image. Only problem now is all the other links below in the same post are showing broken images as the URLs are just links to websites and not actual images. I'm not sure if I need to do an if statement around the image could to determine whether the link is an image link or url link or not.
I was thinking maybe do a separate function, so first use parse to direct the image link to a different function to output the image. But then I don't know if this would work.
$cont = preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembedimage_callback' ), $cont);
PHP
/**
* Passes on any unlinked URLs that are on their own line for potential embedding.
* @param string $contThe content to be searched.
* @return string Potentially modified $content.
*/
function parse( $cont) {
$cont = preg_replace_callback( '|^\s*(https?://[^\s"]+)\s*$|im', array( $this, 'autoembed_callback' ), $cont);
return $cont;
}
/**
* Callback function for {@link AutoEmbed::parse()}.
* @param array $match A regex match array.
* @return string The embed HTML on success, otherwise the original URL.
*/
function autoembed_callback($match) {
$attr['discover'] = true;
$return = $this->get_html( $match[1], $attr );
$match[0] = preg_replace("/\[\/?(.*?)\]/", "", $match[0]);
$match[0] = preg_replace("/\<\/?(.*?)\>/", "", $match[0]);
$image_file = ''.$match[1].'';
$embedded = '<img id="feed" src="'.$image_file.'" onerror="this.style.display=\'none\';" />';
$m = trim(strtolower($match[0]));
$m = str_replace("http://", "", $m);
$m = str_replace("https://", "", $m);
$m = str_replace("ftp://", "", $m);
$m = str_replace("www.", "", $m);
if (strlen($m) > 25) {
$m = substr($m, 0, 25) . "...";
}
if($attr['discover'] = true) {
$return = $this->get_html( $match[1], $attr );
}
return "<a href=\"$match[0]\" target=\"_blank\">$m</a>\n$return\n\n$embedded\n";
}