rozanchetta

[WordPress] [Post-formats] Grab first embed

Jun 15th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. * Este snippet busca o primeiro embed do post, seja através de shortcode (exemplo: [facebook url="https://www.facebook.com/usuario/posts/id-do-post"]
  5. * ou usando o auto embed postando apenas a url (exemplo: https://www.facebook.com/usuario/posts/id-do-post.
  6. * Se for um shortcode, usamos "get_shortcode_regex" (http://codex.wordpress.org/Function_Reference/get_shortcode_regex) e "do_shortcode" (http://codex.wordpress.org/Function_Reference/do_shortcode).
  7. * Se for um autoembed, usamos "wp_oembed_get" (http://codex.wordpress.org/Function_Reference/wp_oembed_get).
  8. */
  9.  
  10. // No functions.php
  11.  
  12. function temabase_get_embed_content() {
  13.     global $post, $posts, $wp_embed;
  14.     $pattern = get_shortcode_regex();    
  15.     ob_start();
  16.     ob_end_clean();
  17.     if ( preg_match_all('/' . $pattern . '/s', $post->post_content, $matches) ) {
  18.         echo do_shortcode($matches [0] [0]);
  19.     }
  20.     elseif (  preg_match_all('|^\s*(https?://[^\s"]+)\s*$|im', $post->post_content, $matches)  )  {
  21.              $embed_code =  wp_oembed_get($matches [1] [0]);            
  22.              echo $embed_code;
  23.     } else {
  24.         echo the_except();
  25.     }
  26. }
  27.  
  28. // No loop (content.php ou similar)
  29.  
  30. echo temabase_get_embed_content();
Advertisement
Add Comment
Please, Sign In to add comment