Удалить точный код вставки (wordpress) с помощью preg_replace

Я использую следующий код, чтобы найти первый YouTube / Vimeo для встраивания в содержание публикации:

function compare_by_offset( $a, $b ) {
return $a['order'] - $b['order'];
}

function first_video_url($post_id = null) {

if ( $post_id == null OR $post_id == '' ) $post_id = get_the_ID();

$post_array = get_post( $post_id );

$markup = $post_array->post_content;

$regexes = array(
'#(?:https?:)?//www\.youtube(?:\-nocookie)?\.com/(?:v|e|embed)/([A-Za-z0-9\-_]+)#', // Comprehensive search for both iFrame and old school embeds
'#(?:https?(?:a|vh?)?://)?(?:www\.)?youtube(?:\-nocookie)?\.com/watch\?.*v=([A-Za-z0-9\-_]+)#', // Any YouTube URL. After http(s) support a or v for Youtube Lyte and v or vh for Smart Youtube plugin
'#(?:https?(?:a|vh?)?://)?youtu\.be/([A-Za-z0-9\-_]+)#', // Any shortened youtu.be URL. After http(s) a or v for Youtube Lyte and v or vh for Smart Youtube plugin
'#<div class="lyte" id="([A-Za-z0-9\-_]+)"#', // YouTube Lyte
'#data-youtube-id="([A-Za-z0-9\-_]+)"#', // LazyYT.js
'#<object[^>]+>.+?http://vimeo\.com/moogaloop.swf\?clip_id=([A-Za-z0-9\-_]+)&.+?</object>#s', // Standard Vimeo embed code
'#(?:https?:)?//player\.vimeo\.com/video/([0-9]+)#', // Vimeo iframe player
'#\[vimeo id=([A-Za-z0-9\-_]+)]#', // JR_embed shortcode
'#\[vimeo clip_id="([A-Za-z0-9\-_]+)"[^>]*]#', // Another shortcode
'#\[vimeo video_id="([A-Za-z0-9\-_]+)"[^>]*]#', // Yet another shortcode
'#(?:https?://)?(?:www\.)?vimeo\.com/([0-9]+)#', // Vimeo URL
'#(?:https?://)?(?:www\.)?vimeo\.com/channels/(?:[A-Za-z0-9]+)/([0-9]+)#' // Channel URL
);$provider_videos = array();

foreach ( $regexes as $regex ) {
if ( preg_match_all( $regex, $markup, $matches, PREG_OFFSET_CAPTURE ) ) {
$provider_videos = array_merge( $provider_videos, $matches[0] );
}
}

if ( empty( $provider_videos ) ) return;

foreach ( $provider_videos as $video ) {
$videos[] = array(
'url'    => $video[0],
'order'  => $video[1]
);
}

usort( $videos, 'compare_by_offset' );

$first_video_url =  current(array_column($videos, 'url'));

if ( empty( $first_video_url ) ) return;

return $first_video_url;

}

Теперь, когда я получил ссылку на первое видео в посте, я хочу удалить его из контента поста. И вот где я застрял. Моя попытка пока:

function remove_first_image ($content) {

$url = first_video_url();
$parsed = parse_url($url);
$video_id = $parsed['query'];
$embed_code = wp_oembed_get($url);

$pattern = 'a pattern for that embed which I fail to make';

$content = preg_replace($pattern, '', $content);

return $content;
}
add_filter('the_content', 'remove_first_image');

Спасибо!

0

Решение

Я думаю, что никто не мог ответить на его собственный глупый вопрос, пока он не спросит его. Здесь приходит ответ:

function remove_first_image ($content) {

if ( is_single() && has_post_format('video') ) {

$url = first_video_url();
$embed_code = wp_oembed_get($url);

$content = str_replace($embed_code, '', $content);
}

return $content;
}

add_filter('the_content', 'remove_first_image');
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector