Advertisement
MrPauloeN

Znacznik YouTube - Optymalizacja SEO YouTube

Jul 17th, 2020 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1. /*
  2. Plugin Name: Znacznik YouTube
  3. Plugin URI: https://www.facebook.com/WPSolucje/
  4. Description: Dodawanie filmu z YouTube. Prosty sposób na "optymalizację" osadzonych treści. Wstawienie znacznika [youtube] spowoduje wygenerowanie kontenera dla miniaturki filmu z YouTube, link bezpośredni i krótki opis. W przypadku większej ilości wstawionych filmików (galeria) ten zabieg może znacząco przyspieszyć ładowanie się strony.
  5. Version: 1.0
  6. Author: Paweł Nowak
  7. Author URI: https://czystespalanie.info
  8. */
  9.  
  10. add_shortcode( 'youtube', 'czystespalanie__youtube' );
  11. function czystespalanie__youtube( $atts, $content = null ){
  12.  
  13.     wp_enqueue_script('lity');
  14.     wp_enqueue_style('lity');
  15.  
  16.     $url = get_stylesheet_directory_uri();
  17.  
  18.         $a = shortcode_atts( array(
  19.         'link' => '',
  20.                 'float'=> ''
  21.  
  22.     ), $atts );
  23.  
  24.     $float = ( $a['float'] ) ? ' float-' . $a['float'] : '';
  25.     );
  26.  
  27.  
  28.     $film_id = preg_match( "/(?<=v=|v\/|vi=|vi\/|youtu.be\/)[a-zA-Z0-9_-]{11}/", $a['link'], $match );
  29.  
  30.     #sprawdzam czy film jest dostępny na podstawie miniaturki
  31.     $headers = @get_headers( 'https://img.youtube.com/vi/' . $match[0] . '/mqdefault.jpg' );
  32.  
  33.  
  34.     if ( strpos( $headers[0], '200') ) {
  35.     $img = '<img src="' . $url . '/images/play.png" class="card-title">';
  36.     } else {
  37.     $img = '<span class="badge badge-danger ml-1 mt-1 p-1">⚠ Film niedostępny</span>';
  38.     }
  39.  
  40.     $output = '<figure class="col-6 col-sm-4 col-md-3 ' . esc_attr( $float ) . '">' . "\r";
  41.     $output .= '<div class="shadow card mb-4 box-shadow rounded-0">' . "\r";;
  42.     $output .= '<a href="' . esc_url( $a['link'] ) . '" data-lity><img class="card-img-top img-fluid rounded-0"  src="https://img.youtube.com/vi/' . $match[0] . '/mqdefault.jpg"/></a>' . "\r";;
  43.     $output .= '<div class="card-img-overlay p-0 '. esc_attr( ( strpos( $headers[0], '200') ) ? '' : ' w-100 text-center' ) . '">';
  44.     $output .= '<a href="' . esc_url( $a['link'] ) .'" data-lity>' . $img . '</a>' . "\r";;
  45.     $output .= '</div>' . "\r";;
  46.     $output .= '<figcaption class="card-body text-center bg-light py-2 px-2">' . "\r";;
  47.     $output .= '<h6 class="h6 card-text font-weight-normal">' . trim( $content ) . '<a title="Link bezpośredni do YouTube" target="_blank" class="external-link float-right mt-3 pt-2" href="' . esc_url( $a['link'] ) . '"></a></h6>' . "\r";;
  48.     $output .= '</figcaption>' . "\r";;
  49.     $output .= '</div>' . "\r";;
  50.     $output .= '</figure>' . "\r";;
  51.  
  52.     return $output;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement