2 Snippets for wpForo Plugin You Can’t Live Without
A snippet is a small chunk of PHP code that you can use to extend the functionality of a WordPress-powered website; essentially a mini-plugin with less load on your site.
Most snippet-hosting sites tell you to add snippet code to your active theme’s
functions.php
file, which can get rather long and messy after a while. Not to mention that after every update, you will have to do the changes again.
So the easiest and best way to add snippets is using the Code Snippets WordPress plugin.
wpForo is the best plugin, if you need to have a Forum for your WordPress site. Here are two small snippets of php code, that will greatly enhance your Forums.
The first is for embedding Youtube videos (instead of a link). The second is for embedding the actual image attachment inside your post.
For Youtube videos links:
add_filter('wpforo_content_after', 'wpforo_custom_video_embed', 10);
function wpforo_custom_video_embed($content)
{
$paterns = array();
$paterns[] = "/<a[^><]+>\s*[a-zA-Z\/\/:\.]*youtube.com\/watch\?v=([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i";
$paterns[] = "/<a[^><]+>\s*[a-zA-Z\/\/:\.]*youtu.be\/([a-zA-Z0-9\-_]+)([a-zA-Z0-9\/\*\-\_\?\&\;\%\=\.]*)/i";
$content = preg_replace($paterns, "<iframe width=\"420\" height=\"280\" src=\"//www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe>", $content);
return $content;
}
For Image attachments:
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);