¿Cómo permitir imágenes en lugar de URL en los comentarios de WordPress?
De forma predeterminada, las direcciones URL del comentario se mantienen como están. Si un usuario coloca directamente una URL de imagen, sería bueno reemplazar la URL con la etiqueta HTML img real. Agregue la siguiente pieza de código PHP en su tema de wordpress functions.php :
// 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');
Usamos una expresión regular para encontrar las URL de las imágenes (que terminan con las extensiones jpg/png/jpeg/gif/bmp) y las reemplazamos con la etiqueta HTML img. También tenemos que permitir la etiqueta IMG en la sección de comentarios, para que se muestre correctamente. La función anterior debe agregarse al filtro preprocess_comment que se invoca antes de que se inserte un comentario en la base de datos.