WordPress自动删除youtube删除的视频

时间:2018-08-25 04:00:09

标签: php wordpress youtube categories

我有一个网站,可以从YouTube自动导入视频。 并且我已经在(single.php中使用了这些代码,因为它无法在functions.php中使用)检查视频是否已删除,然后将帖子重定向到其类别页面

这样的帖子

<iframe width="560" height="315" src="https://www.youtube.com/embed/Videocode" frameborder="0"></iframe>

代码

    //Check If Video Is Deleted
    global $post;
    global $wpdb;

    $categories = get_the_category($post->ID);
    $cat_link = get_category_link($categories[0]->cat_ID);

    the_post(); // necessary to use get_the_content()

    $links = [get_the_content(get_the_ID())];
    foreach($links as $link)
    preg_match("#([\/|\?|&]vi?[\/|=]|youtu\.be\/|embed\/)([a-zA-Z0-9_-]+)#", $link, $matches);
    //var_dump(end($matches));
    $key = (end($matches));

    $headers = get_headers('https://www.youtube.com/oembed?format=json&url=http://www.youtube.com/watch?v=' . $key);

    if(is_array($headers) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$headers[0]) : false){
    // video exists
    //echo 'found ID';

    } else {
    // video does not exist
    // Try to mark the post by meta value or tag but not work
    // update_post_meta(get_the_ID(),'deletedxxx','yes');
    //wp_set_post_terms( $post_id, 'deleted', $taxonomy );
    wp_redirect($cat_link,301); exit;
    }

我正在尝试通过元值或标记来标记帖子,但是不起作用! 我需要两件事

1- 通过元值标记帖子,因为

A-(如果具有此值,请停止检查视频状态)。

B-(如果具有此值,则将其重定向到具有任何标签的任何帖子)。

2- 从主题的任何循环中删除此帖子

谢谢

0 个答案:

没有答案