J82
J82

Reputation: 8457

PHP in href not working

In the first line, $embedded_video_url is not outputting anything. It just says <a class="colorbox-video cboxElement" href="">Pop Up</a>. Is it because the variable $embedded_video_url is defined later in the code? Do I have to define the variable before the h3 element in the first line?

<h3 class="widget-title">Latest Video <span class="pop-up-link"><a class="colorbox-video cboxElement" href="<?php echo $embedded_video_url ?>">Pop Up</a></span></h3>
<?php   
    $args = array( 
        'numberposts' => '1', 
        'tax_query' => array(
            array(
                'taxonomy' => 'post_format',
                'field' => 'slug',
                'terms' => 'post-format-video'
            )
        ),
        'meta_query' => array(
            array(
                'key' => 'dt_video',
                'value' => '',
                'compare' => '!='
            )
        )
    );
    $latest_video = wp_get_recent_posts($args); // Get latest video in 'video' post format
    $latest_video_id = $latest_video['0']['ID']; // Get latest video ID
    $video_url = "http://www.youtube.com/watch?v=l4X2hQC32NA&feature=g-all-u&context=G258729eFAAAAAAAAHAA?rel=0";
    $search  = '#(?:href="https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com(?:\/embed\/|\/v\/|\/watch?.*?v=))([\w\-]{10,12}).*$#x';
    $replace = "www.youtube.com/embed/$1";
    preg_match_all($search, $video_url, $matches);
    $embedded_video_url = preg_replace($search, $replace, $video_url) ;
    echo '<iframe width="180" height="101" src="'.$embedded_video_url.'" frameborder="0" allowfullscreen></iframe>';
?>

Upvotes: 0

Views: 100

Answers (1)

Jeff Lambert
Jeff Lambert

Reputation: 24661

Yes. You should define / initialize your variables before you use them. If you don't they will be empty. If you move the first line down to the bottom it should fix your issue.

Upvotes: 3

Related Questions