Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to use the youtube API on two of my template pages. Obviously I want to enqueue it correctly. When I put this code in my footer.php page, I got it to work:

<script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
<script src="https://apis.google.com/js/plusone.js"></script>
<script type="text/javascript">
function vidinfolist(data) {
    var dat = data.feed.entry;
    var pageOutput = '<ul>';
    for (var i=0; i<data.feed.entry.length; i++) {
    var datID=dat[i].id.$t.substring(38);
    var datTitle=dat[i].title.$t;
    var datDescription=dat[i].media$group.media$description.$t;
    var datThumbnail=dat[i].media$group.media$thumbnail[0].url;
    pageOutput += '<li><h2>' + datTitle + '</h2>';
    pageOutput += '<li><p>' + datDescription + '</p>';
    pageOutput += '<div class="g-ytsubscribe" data-channel="droptroucomedy" data-layout="default"></div>';
    pageOutput+='<iframe width="952" height="540" src="http://www.youtube.com/embed/'+datID+'?wmode=transparent&amp;HD=0&amp;rel=0&amp;showinfo=0&amp;controls=1&amp;fs=1&amp;autoplay=0" width= frameborder="0" allowfullscreen></iframe></li>';
    }
    document.getElementById('videoCode').innerHTML = pageOutput;
    pageOutput +='</ul>';
}
</script>
<script type="text/javascript" src="http://gdata.youtube.com/feeds/users/droptroucomedy/uploads?alt=json-in-script&callback=vidinfolist&max-results=1"></script>

Seeing that it works, I deleted it and placed the following code in my function.php file. My youtube_js.js file is down below. It's not working, and I'm not sure why, any suggestions? Am I supposed to add anything in my template-name.php files?

add_action ( 'wp_enqueue_scripts', 'dtrou_youtube_api' );

function dtrou_youtube_api() {
    wp_register_script( 'dtrou-youtube', get_template_directory_uri() . '/js/youtube_js.js', array('jquery'), '', true );
    wp_register_script( 'youtube-subscribe-btn', 'https://apis.google.com/js/plusone.js', array(), '', true );
    wp_register_script( 'youtube-subscribe-featured', 'http://gdata.youtube.com/feeds/users/droptroucomedy/uploads?alt=json-in-script&callback=vidinfolist&max-results=1', array(), '', true );
    wp_register_script( 'youtube-subscribe-videos', 'http://gdata.youtube.com/feeds/users/droptroucomedy/uploads?alt=json-in-script&callback=vidinfolist&max-results=100', array(), '', true );

if ( is_page_template('t-featured.php') ) {
    wp_enqueue_script('dtrou-youtube');
    wp_enqueue_script('youtube-subscribe-featured');
    wp_enqueue_script('youtube-subscribe-btn');
}

if ( is_page_template('t-videos.php') ) {
    wp_enqueue_script('dtrou-youtube');
    wp_enqueue_script('youtube-subscribe-videos');
    wp_enqueue_script('youtube-subscribe-btn');
}
}

youtube_js.js file.

function vidinfolist(data) {
var dat = data.feed.entry;
var pageOutput = '<ul>';
for (var i=0; i<data.feed.entry.length; i++) {
var datID=dat[i].id.$t.substring(38);
var datTitle=dat[i].title.$t;
var datDescription=dat[i].media$group.media$description.$t;
var datThumbnail=dat[i].media$group.media$thumbnail[0].url;
pageOutput += '<li><h2>' + datTitle + '</h2>';
pageOutput += '<li><p>' + datDescription + '</p>';
pageOutput += '<div class="g-ytsubscribe" data-channel="droptroucomedy" data-layout="default"></div>';
pageOutput+='<iframe width="952" height="540" src="http://www.youtube.com/embed/'+datID+'?wmode=transparent&amp;HD=0&amp;rel=0&amp;showinfo=0&amp;controls=1&amp;fs=1&amp;autoplay=0" width= frameborder="0" allowfullscreen></iframe></li>';
}
document.getElementById('videoCode').innerHTML = pageOutput;
pageOutput +='</ul>';
}
share|improve this question
    
Solved. Needed to place <?php wp_footer(); ?> in my footer.php file. –  vytfla Nov 6 '13 at 4:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.