Как стилизовать заглушку видео www.youtube.com на сайте .ru?
не открывает видео на моем сайте с ютуба. Как исправить?
Этот PHP-скрипт выполняет три действия:
- Перехватывает текст статьи на сервере до того, как он будет отправлен в браузер пользователя.
- Находит в тексте все теги < iframe >, у которых в адресе (src) упоминается youtube.com.
- Заменяет каждый найденный iframe на аккуратный HTML-блок с названием видео, текстовым уведомлением о недоступности и прямой ссылкой на оригинал.
1. Добавьте этот код в functions.php
add_filter('the_content', 'replace_youtube_iframe_with_stub'); function replace_youtube_iframe_with_stub($content) { // Ищем любой iframe, у которого в src упоминается youtube.com $pattern = '/<iframe[^>]*src=["\']([^"\']*youtube\.com[^"\']*)["\'][^>]*>(?:\s*<\/iframe>)?/i'; $content = preg_replace_callback($pattern, function($matches) { $src = $matches[1]; // Пытаемся извлечь ID видео (поддерживает watch?v=, /embed/, /shorts/, /v/) preg_match('/(?:v=|\/embed\/|\/shorts\/|\/v\/)([a-zA-Z0-9_-]{11})/', $src, $id_matches); $video_id = isset($id_matches[1]) ? $id_matches[1] : ''; // Пытаемся найти title в исходном теге iframe preg_match('/title=["\']([^"\']*)["\']/i', $matches[0], $title_matches); $title = isset($title_matches[1]) && !empty($title_matches[1]) ? $title_matches[1] : 'Видео'; $stub_html = '<div class="youtube-stub">'; $stub_html .= '<div class="youtube-stub__content">'; $stub_html .= '<h3>' . esc_html($title) . '</h3>'; $stub_html .= '<p>Просмотр видео на сайте временно недоступен из-за ограничений доступа к YouTube.</p>'; if ($video_id) { $stub_html .= '<a href="https://www.youtube.com/watch?v=' . esc_attr($video_id) . '" target="_blank" rel="noopener noreferrer" class="youtube-stub__link">Открыть видео на YouTube</a>'; } else { $stub_html .= '<a href="' . esc_url($src) . '" target="_blank" rel="noopener noreferrer" class="youtube-stub__link">Открыть ссылку на YouTube</a>'; } $stub_html .= '</div>'; $stub_html .= '</div>'; return $stub_html; }, $content); return $content; }2. CSS стили
.youtube-stub { background-color: #f9f9f9; border: 1px solid #e0e0e0; border-radius: 6px; padding: 32px 24px; text-align: center; max-width: 100%; margin: 20px auto; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; box-sizing: border-box; } .youtube-stub__content h3 { margin-top: 0; margin-bottom: 16px; font-size: 18px; font-weight: 600; color: #333333; line-height: 1.4; } .youtube-stub__content p { color: #666666; margin-bottom: 24px; font-size: 15px; line-height: 1.5; } .youtube-stub__link { display: inline-block; background-color: #cc0000; color: #ffffff; text-decoration: none; padding: 12px 24px; border-radius: 4px; font-weight: 500; font-size: 15px; transition: background-color 0.2s ease; } .youtube-stub__link:hover { background-color: #990000; text-decoration: none; color: #ffffff; }
В итоге взамен битого блока у вас на сайте будет такой красивый аккуратный блок в месте вставки видео с ютуба.
стрелками указал как будет выглядеть
