Kuidas lubada WordPressi kommentaarides URL-i asemel pilte?
Vaikimisi jäetakse kommentaaris olevad URL-id nii nagu nad on. Kui kasutaja lisab otse pildi URL-i, oleks tore asendada URL tegeliku HTML-i img-sildiga. Lisage oma WordPressi teemasse functions.php järgmine PHP-koodi osa :
// empty - allow all posts, otherwise, put post IDs joined by comma
define('ALLOW_POSTS', '');
function helloacm_allow_comment_image( $comment) {
$post_ID = $comment["comment_post_ID"];
$allow_posts = ALLOW_POSTS? explode(',', ALLOW_POSTS): array();
if(empty($allow_posts) || in_array($post_ID, $allow_posts)){
global $allowedtags;
$content = $comment["comment_content"];
$content = preg_replace('/(https?://S+.(?:jpg|png|jpeg|gif|bmp))+/','<img title="$0 How to Allow Images Instead of URL in WordPress Comments? wordpress " src="$0" alt="$0 How to Allow Images Instead of URL in WordPress Comments? wordpress " />',$content);
$allowedtags['img'] = array('src' => array(), 'alt' => array());
$comment["comment_content"] = $content;
}
return $comment;
}
add_filter('preprocess_comment', 'helloacm_allow_comment_image');
Kasutame piltide URL-ide leidmiseks regulaaravaldist (lõpevad laienditega jpg/png/jpeg/gif/bmp) ja asendame selle HTML-i img-sildiga. Samuti peame lubama IMG-märgendi kommentaaride jaotises, et seda õigesti kuvada. Ülaltoodud funktsioon tuleb lisada filtrile preprocess_comment, mis käivitatakse enne kommentaari sisestamist andmebaasi.