Як дозволити зображення замість URL-адреси в коментарях WordPress?
За замовчуванням URL-адреси в коментарях залишаються без змін. Якщо користувач безпосередньо вводить URL-адресу зображення, було б непогано замінити URL-адресу справжнім HTML-тегом img. Додайте наступний фрагмент коду PHP у свою тему 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');
Ми використовуємо регулярний вираз для пошуку URL-адрес зображень (закінчуються розширеннями jpg/png/jpeg/gif/bmp) і замінюємо тегом HTML img. Ми також маємо дозволити тег IMG у розділі коментарів, щоб правильно відображати його. Наведену вище функцію потрібно додати до фільтра preprocess_comment, який викликається перед вставленням коментаря в базу даних.