• Resolved Dana S

    (@dana-s)


    Hello,

    A while ago I implemented a solution you provided to another users request, which was making a link of the preview thumbnail instead of playing on click.

    I’m referring to this thread:

    https://wordpress.org/support/topic/make-link-of-thumbnail-instead-of-playing-onclick?replies=2″

    After all the major changes proceeding youtubes drop of v2 api, I’m no longer able to get this to work. I believe it’s due to a change in the api.php file, but I could be wrong.

    To make a long story short, I would still like to be able to have the thumbnails link rather than playing onclick.

    Is this still possible?

    The following is the code that used to work, but no longer does:

    /* Load the "Related YouTube Videos" API class if it does not exist yet. */
    if( !class_exists( 'RelatedYouTubeVideos_API' ) ) {
    
      $file = str_replace( '/', DIRECTORY_SEPARATOR, ABSPATH ) . 'lib' . DIRECTORY_SEPARATOR . 'RelatedYouTubeVidoes' . DIRECTORY_SEPARATOR . 'API.php';
    
      if( file_exists( $file ) ) {
    
        include_once $file;
    
      }
    
    }
    /* Only continue if the API class could be loaded properly. */
    if( class_exists( 'RelatedYouTubeVideos_API' ) ) {
    
      $RytvAPI  = new RelatedYouTubeVideos_API();
    
      /* Do your configuration */
      $data = $RytvAPI->validateConfiguration(
        array(
          'relation'  => 'postTitle',
          'max'       => '3',
          'width'     => 150,
          'height'    => 150
        )
      );
    
      /* Search YouTube. */
      $results  = $RytvAPI->searchYouTube( $data );
    
      /* Create your very own custom HTML */
      if( !empty( $results ) ) {
    
        foreach( $results as $video ) {
    
          // Try detecting the YouTube Video ID
          $videoID = ( preg_match( '#\?v=([^&]*)&#i', $video->link['href'], $match ) && isset( $match[1] ) ) ? (string) $match[1] : '';
    
          echo '<a href="' . $video->link['href'] . '"><img src="http://img.youtube.com/vi/' . $videoID . '/0.jpg" alt="" width="150" height="150" /></a>' . "\n";
    
        }
    
      }
    
    }

    I’m using your latest release of Related Youtube Videos by the way. And, this may be obvious, but I’m trying to hard code this functionality in a theme I’m using. I appreciate any help in advance.

    Thanks, Dana

    https://wordpress.org/plugins/related-youtube-videos/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Chris

    (@zenation)

    Hi Dana,
    the good news is that it should still be possible. But you’re right, you have to change the code snippet a bit.

    /* Load the "Related YouTube Videos" API class if it does not exist yet. */
    if( !class_exists( 'RelatedYouTubeVideos_API' ) ) {
    
      $file = str_replace( '/', DIRECTORY_SEPARATOR, ABSPATH ) . 'lib' . DIRECTORY_SEPARATOR . 'RelatedYouTubeVidoes' . DIRECTORY_SEPARATOR . 'API.php';
    
      if( file_exists( $file ) ) {
    
        include_once $file;
    
      }
    
    }
    /* Only continue if the API class could be loaded properly. */
    if( class_exists( 'RelatedYouTubeVideos_API' ) ) {
    
      $RytvAPI  = new RelatedYouTubeVideos_API();
    
      /* Do your configuration */
      $data = $RytvAPI->validateConfiguration(
        array(
          'relation'  => 'postTitle',
          'max'       => '3',
          'width'     => 150,
          'height'    => 150
        )
      );
    
      /* Search YouTube. */
      $results  = $RytvAPI->searchYouTube( $data );
    
      /* Create your very own custom HTML */
      if( !empty( $results ) ) {
    
        foreach( $results as $video ) {
    
          $videoID    = ( isset( $video['videoID'] ) ) ? $video['videoID'] : '';
    
          $videoLink  = ( $videoID !== '' ) ? 'https://www.youtube.com/watch?v=' . $videoID : 'javascript:void(0);';
    
          echo '<a href="' . $videoLink . '"><img src="http://img.youtube.com/vi/' . $videoID . '/0.jpg" alt="" width="150" height="150" /></a>' . "\n";
    
        }
    
      }
    
    }

    It’s actually only a change of two lines (within the last foreach loop) but I’ve re-posted the full code for the bigger picture, so to speak 😉

    Could you test if this working for you?

    Thread Starter Dana S

    (@dana-s)

    Hi Chris,

    I tried the new code and it works perfectly. Thank you so much.

    I have another question related to the code above, but I figured I should create a new thread for it, which I’ll do now.

    Thanks,
    Dana

    Thread Starter Dana S

    (@dana-s)

    I just have one more request regarding the new code you provided. Everything still works fine, but I was curious if there a way to display the youtube video title as the alt or title tag for preview images?

    Something like this:

    ... echo '<a href="' . $videoLink . '"><img src="http://img.youtube.com/vi/' . $videoID . '/0.jpg" title="$videoTitle" width="150" height="150" /></a>' . "\n";
    ....

    Thanks in advance for any help,
    Dana

    Plugin Author Chris

    (@zenation)

    You’re close 😉

    echo '<a href="' . $videoLink . '"><img src="http://img.youtube.com/vi/' . $videoID . '/0.jpg" title="' . $video['title'] . '" width="150" height="150" /></a>' . "\n";

    Thread Starter Dana S

    (@dana-s)

    Awesome thanks again!

    hi, please explain…

    is possible to make eg. 2 rows of 8 video thumbnails (4 x 2 rows)… which then I click with another plugin that loads them in lightbox…

    instead of directly embedding the videos?

    that was possible with a plugin called “subzane youtube” that now is not available anymore in WordPress…

    thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Re-Request: Make link of thumbnail instead of playing onclick’ is closed to new replies.