Wie erlaube ich Bilder anstelle von URLs in WordPress-Kommentaren?
Standardmäßig bleiben die URLs im Kommentar unverändert. Wenn ein Benutzer direkt eine Bild-URL eingibt, wäre es schön, die URL durch das eigentliche HTML-img-Tag zu ersetzen. Fügen Sie den folgenden PHP-Code in Ihre WordPress -Design- functions.php ein :
// 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');
Wir verwenden einen regulären Ausdruck, um Bild-URLs (die mit den Erweiterungen jpg/png/jpeg/gif/bmp enden) zu finden und durch das HTML-Tag img zu ersetzen. Außerdem müssen wir das IMG-Tag im Kommentarbereich zulassen, um es korrekt anzuzeigen. Die obige Funktion muss dem Filter preprocess_comment hinzugefügt werden, der aufgerufen wird, bevor ein Kommentar in die Datenbank eingefügt wird .