filters - How can I replace an oEmbed URL in post with actual embed HTML

admin2025-06-05  2

What I want is to get the output of the Twitter oEmbed system into my actual posts. IMHO the embed output itself is high-quality, semantic HTML and I'd rather have it inside the post than in the embed metadata in case their js stops working (in which case the tweet would display as a sensible blockquote).

So what I need is to hijack the oEmbed system and, upon processing it, replace the embed URL in the post with the HTML from Twitter. Clearly the solution should also be able to handle other kinds of embeds.

UPDATE: Just to be clear, I know how oEmbed works by default (i.e. the URL will be replaced with the oEmbed HTML when the 'the_content' filter is run on the post content) and that is not what I want. What I need is a way to hijack that system and, instead of only having the content replaced at display time, have the embed URL itself replaced inside the post_content in the the database with the HTML returned from the oEmbed provider.

What I want is to get the output of the Twitter oEmbed system into my actual posts. IMHO the embed output itself is high-quality, semantic HTML and I'd rather have it inside the post than in the embed metadata in case their js stops working (in which case the tweet would display as a sensible blockquote).

So what I need is to hijack the oEmbed system and, upon processing it, replace the embed URL in the post with the HTML from Twitter. Clearly the solution should also be able to handle other kinds of embeds.

UPDATE: Just to be clear, I know how oEmbed works by default (i.e. the URL will be replaced with the oEmbed HTML when the 'the_content' filter is run on the post content) and that is not what I want. What I need is a way to hijack that system and, instead of only having the content replaced at display time, have the embed URL itself replaced inside the post_content in the the database with the HTML returned from the oEmbed provider.

Share Improve this question edited Jul 12, 2013 at 15:11 jerclarke asked Jul 11, 2013 at 17:30 jerclarkejerclarke 3,0832 gold badges28 silver badges35 bronze badges 1
  • 1 Did you ever solve this? Chances are you just need to parse the post content on save and replace the links with the resulting code yourself. There should be regEx to find oEmbed URLs, then another function to force generate the response from the URL. You would only need to swap them at that point. The downside is that 3 years later in the tech world produces a completely different embed usually. So all your hard work will be obsolete at some point. – jgraup Commented Nov 8, 2016 at 12:14
Add a comment  | 

2 Answers 2

Reset to default 1

Check out the embed API, particularly the functions wp_oembed_add_provider() and wp_embed_register_handler().

What version of WP are you running at the moment? And what happens when you place a Twitter URL on its own line in plain text? 3.4+ should handle embeds, never tried it myself.

This will be copyright violation and it is very not recommended to do from a legal perspective.

From a technical perspective, embeded code many times uses "external" resources like JS and CSS that might load in the context of an iframe but not in the context of your site.

So, it is both a legal and technical mine field which you probably should not even attempt to step in.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749089624a316273.html

最新回复(0)