创建简单的基于oEmbed的WordPress短代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建简单的基于oEmbed的WordPress短代码相关的知识,希望对你有一定的参考价值。

Say you wanted to create a shortcode like this: [youtube id="991WcoEPwb8"]

And instead of manually creating the html yourself, you wanted to use YouTube’s oEmbed provider to get the HTML. While that’s easy to do using WordPress’ existing functions (wp_oembed_get() for example), you must implement your own caching of the result as WordPress’ oEmbed class does not include any caching of it’s own.

However, WordPress comes with an [embed] shortcode that’s also secretly used for the shortcode-less embeds. A cool trick is to make that shortcode’s existing code (complete with caching) work for you! This post explains how to do it.
  1. add_shortcode( 'youtube', 'my_youtube_shortcode' );
  2.  
  3. function my_youtube_shortcode( $atts ) {
  4.  
  5. // We need to use the WP_Embed class instance
  6. global $wp_embed;
  7.  
  8. // The "id" parameter is required
  9. if ( empty($atts['id']) )
  10. return '';
  11.  
  12. // Construct the YouTube URL
  13. $url = 'http://www.youtube.com/watch?v=' . $atts['id'];
  14.  
  15. // Run the URL through the handler.
  16. // This handler handles calling the oEmbed class
  17. // and more importantly will also do the caching!
  18. return $wp_embed->shortcode( $atts, $url );
  19. }

以上是关于创建简单的基于oEmbed的WordPress短代码的主要内容,如果未能解决你的问题,请参考以下文章

php WordPress oEmbed过滤器

php 将包装器div添加到oEmbed WordPress对象

php 将一些参数过滤到WordPress YouTube oEmbed请求中。启用适度品牌,隐藏YouTube徽标。删除视频标题

wordpress付费阅读用啥插件及支付方式?

基于 LNMP 快速简单搭建 wordpress 平台

如何在 VueJs 组件中对 oembed javascript 标记进行整数处理?