• Resolved saphod

    (@saphod)


    Hi,

    sometimes, feeds do not show the embedded YouTube videos. In that case, I would like to automatically add a small text above the video that says:

    “There should be an embedded flash video visible below. If it does not show in your feed reader, please visit the original post to view it.”

    As I said, I would like to do that automatically and just when a feed is shown. I managed to integate that into the Kimili Flash Embed plugin. Does anyone know how I can accomplish this with the built-in (auto) embed function of WordPress where you only need the URL or the shortcode [embed]URL[/embed] to embed the video?

    I wonder if there is a hook or filter for that function or where that function is being executed during the parsing of the content. Can anyone tell me where in the WordPress source (e.g. file and line number) I can find that function? Is there a difference in the function if it is a normal post or a feed?

    Pointing me to the source code should be a great first step, thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Not sure specifically what you need, but i’d suggest starting with a look at wp-includes/class-oembed.php

    You’ll also find references to the embed class in wp-includes/media.php.

    Hope that helps nudge you in the right direction… ??

    Thread Starter saphod

    (@saphod)

    Hi t31os_,

    I think pointing me to the wp-includes/class-oembed.php was a really good idea… in line 261f (WP 3.0), it says:

    // You can use this filter to add support for custom data types or to filter the result
    		return apply_filters( 'oembed_dataparse', $return, $data, $url );

    I’d just have to use the add_filter()-function with oembed_dataparse. It appears to me as if $data contains the embed code. I’d just need to add some text before to leave the little note, like $data = "(note....) " . $data, but the problem is that I just want to do that in a feed. Maybe, I can check with is_feed() if a post or a feed is delivered?!

    I found something similar in the support forums:
    https://www.ads-software.com/support/topic/412206

    But there is another filter in class_oembed.php on line 99 as I just found out:

    return apply_filters( 'oembed_result', $this->data2html( $data, $url ), $url, $args );

    Could I use that, too, and if so, how?

    Thread Starter saphod

    (@saphod)

    Correction:

    In class-oembed.php, it says for the function data2html()(which provides the oembed_dataparse filter) on line 233:

    * @return bool|string False on error, otherwise the HTML needed to embed.

    So, my solution could be something like this, right?

    function add_video_embed_note($return, $data, $url) {
    
         /* Do nothing if an error occured while parsing */
         if(!$return) return false;
    
         /* Add note if it is a video in a feed */
         if(is_feed() && $data->$type=='video') {
              $note_to_add = "<p><small>There should be an embedded flash video visible below.<br />If it does not show in your feed reader, please visit the original post to view it.</small></p>";
              $return = $note_to_add . $return;
         }
    
         /* Return the result */
         return $return;
    }
    add_filter('oembed_dataparse', 'add_video_embed_note', 10, 3);

    Hmm… could it really be that simple? I have to try that out within the next days – could be turned into a little plugin… unfortunately, I have no time at the moment. Fortunately, I have written down the code here so I will remember. ??

    I’d say try it and see.. ??

    Let me know how you get on..

    Thread Starter saphod

    (@saphod)

    OK, I’ve tried it, but it doesn’t work. :’-(

    Actually, it has to be $data->type instead of $data->$type, I reckon, but that doesn’t make a difference, anyway.

    Even if I leave the if-clause out (which leads to the result that the note should ALWAYS be shown, not only in a feed and not only with a video)… the content will not change – THERE IS NO NOTE!

    What am I missing?

    Once again:
    I use the “auto embed”-function, which means that I only put the YouTube-URL on its own on a single line – I do NOT use the [embed]-shortcode… but it should be the same, shouldn’t it?

    I also tried to use a class as suggested here:
    https://www.ads-software.com/support/topic/412206

    No note.

    Does anyone know what’s wrong with my code?

    Thread Starter saphod

    (@saphod)

    After some searching, I found a different filter to use:
    embed_oembed_html in wp-includes/media.php.

    Since it does not provide a $data-object to check wether it is a video or an image, I had to use the strpos-function to see if the generated html uses the embed-tags.

    And here is the actual working code:

    function add_video_embed_note($html, $url, $attr) {
    
         if (is_feed() && strpos($html, "<embed src=" ) !== false) {
              return $note_to_add = "<p><small>There should be an embedded flash video visible below.<br />If it does not show in your feed reader, please visit the original post to view it.</small></p>" . $html;
         } else {
              return $html;
         }
    
    }
    add_filter('embed_oembed_html', 'add_video_embed_note', 10, 3);

    You can download a plugin here:
    https://www.saphod.net/2010/07/06/auto-embedded-videos-and-rss-feeds/

    I need to get a title above an embedded youtube video. Is this possible? Can anyone help me? I tried what nootron said to do on https://www.ads-software.com/support/topic/filter-hook-for-built-in-oembed-providers-eg-youtube but it didn’t work. Where do I put that code?

    -Thank you!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Hook/Filter for auto embed function of WP?’ is closed to new replies.