【问题标题】:How to determine whether my content has a instagram embed?如何确定我的内容是否嵌入了 Instagram?
【发布时间】:2017-01-30 13:14:52
【问题描述】:

我网站中的帖子包含来自以下任何嵌入的视频(单个)。

  1. YouTube
  2. 脸书
  3. Instagram

我的问题是,在前端获取它们时,我想知道我的内容是否有嵌入,如果有,则嵌入以下哪个。 (iframe 存在检查是一种(肮脏的)方式,它仍然可以为 instagram 工作

PHP代码:

    $video_start = strpos($singlePost->post_content, "<iframe");//Get to the start of the iframe(video)
    $video_stop = strpos($singlePost->post_content, "</iframe>");//Get to the end of the iframe(video)
    $iframe_content = substr($singlePost->post_content, $video_start, $video_stop);
    $xpath = new DOMXPath(@DOMDocument::loadHTML($iframe_content));
    $iframe_src = $xpath->evaluate("string(//iframe/@src)");
    $parsed_url = parse_url($iframe_src);
    $host = $parsed_url['host'];

    if(strpos($host, "youtube") !== false) { // If it is a youtube video append this
        $iframe_src = $iframe_src."?rel=0";// This option has to be appended of youtube URL's
        $related_social_icon = "youtube";
        $related_social_media = "youtube";
    }

<iframe class="<?php echo $iframe_class; ?>" src="<?php echo $iframe_src; ?>" style="background-size: cover;" allowfullscreen></iframe>

以上代码适用于 youtube,但不适用于 instagram,因为当插入 instagram 作为块引用标签时,如果您回显它们,由于其中的脚本,它将立即成为 iframe 标签。

【问题讨论】:

  • 先生们,感谢这个负面的礼物,请告诉我是什么让我的帖子配得上这个礼物。我不认为这方面存在问题。
  • 老实说这个问题太狭窄了。您尝试过的事情没有任何努力的迹象或发布的代码。这些东西就是 stackoverflow 的用途。

标签: php wordpress facebook youtube instagram


【解决方案1】:

我会选择这样的:

add_filter('the_content', function($content) {
    $identifier = '<embed';
    if (strpos($content, $identifier) !== false) {
        // identifier found
        $content = '<h1>This page includes an embed</h1>'.$content;
    }
    return $content;
});

我不确定你的嵌入是什么样子的,你说的是 iframe。所以你需要找到一些你可以检查的标识符。

您的帖子可能被否决了,因为它可能包含更多信息?

【讨论】:

  • 我用的和你做的一样,我用的是iframe。但是在instagram上,放置的内容是blockquote,但收到的是iframe,这就是我卡住的地方。
猜你喜欢
  • 2016-06-16
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多