<small id='mlFjV'></small><noframes id='mlFjV'>

  • <tfoot id='mlFjV'></tfoot>
    <legend id='mlFjV'><style id='mlFjV'><dir id='mlFjV'><q id='mlFjV'></q></dir></style></legend>
  • <i id='mlFjV'><tr id='mlFjV'><dt id='mlFjV'><q id='mlFjV'><span id='mlFjV'><b id='mlFjV'><form id='mlFjV'><ins id='mlFjV'></ins><ul id='mlFjV'></ul><sub id='mlFjV'></sub></form><legend id='mlFjV'></legend><bdo id='mlFjV'><pre id='mlFjV'><center id='mlFjV'></center></pre></bdo></b><th id='mlFjV'></th></span></q></dt></tr></i><div id='mlFjV'><tfoot id='mlFjV'></tfoot><dl id='mlFjV'><fieldset id='mlFjV'></fieldset></dl></div>

          <bdo id='mlFjV'></bdo><ul id='mlFjV'></ul>
      1. YouTube iframe API 和 WordPress

        YouTube iframe API and WordPress(YouTube iframe API 和 WordPress)
          <bdo id='kPuzo'></bdo><ul id='kPuzo'></ul>
                <legend id='kPuzo'><style id='kPuzo'><dir id='kPuzo'><q id='kPuzo'></q></dir></style></legend>
              • <small id='kPuzo'></small><noframes id='kPuzo'>

                  <tbody id='kPuzo'></tbody>
                <i id='kPuzo'><tr id='kPuzo'><dt id='kPuzo'><q id='kPuzo'><span id='kPuzo'><b id='kPuzo'><form id='kPuzo'><ins id='kPuzo'></ins><ul id='kPuzo'></ul><sub id='kPuzo'></sub></form><legend id='kPuzo'></legend><bdo id='kPuzo'><pre id='kPuzo'><center id='kPuzo'></center></pre></bdo></b><th id='kPuzo'></th></span></q></dt></tr></i><div id='kPuzo'><tfoot id='kPuzo'></tfoot><dl id='kPuzo'><fieldset id='kPuzo'></fieldset></dl></div>

              • <tfoot id='kPuzo'></tfoot>
                  本文介绍了YouTube iframe API 和 WordPress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在 WordPress 帖子页面上尝试以下代码:

                  I'm trying the following code on WordPress post page:

                  <div id="player"></div>
                  
                  <script>
                      // 2. This code loads the IFrame Player API code asynchronously.
                      var tag = document.createElement('script');
                  
                      tag.src = "https://www.youtube.com/iframe_api";
                      var firstScriptTag = document.getElementsByTagName('script')[0];
                      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
                  
                      // 3. This function creates an <iframe> (and YouTube player)
                      //    after the API code downloads.
                      var player;
                      function onYouTubeIframeAPIReady() {
                          player = new YT.Player('player', {
                              height: '390',
                              width: '640',
                              videoId: 'M7lc1UVf-VE',
                              playerVars: { 'autoplay': 1, 'controls': 0 },
                              events: {
                                  'onReady': onPlayerReady,
                                  'onStateChange': onPlayerStateChange
                              }
                          });
                      }
                  
                      // 4. The API will call this function when the video player is ready.
                      function onPlayerReady(event) {
                          event.target.playVideo();
                      }
                  
                      // 5. The API calls this function when the player's state changes.
                      //    The function indicates that when playing a video (state=1),
                      //    the player should play for six seconds and then stop.
                      var done = false;
                      function onPlayerStateChange(event) {
                          if (event.data == YT.PlayerState.PLAYING && !done) {
                              setTimeout(stopVideo, 6000);
                              done = true;
                          }
                      }
                      function playVideo() {
                          player.playVideo();
                      }
                      function pauseVideo() {
                          player.pauseVideo();
                      }
                      function stopVideo() {
                          player.stopVideo();
                      }
                      function loadVideoById(val) {
                          player.loadVideoById(val, 0, "large");
                      }
                  </script>
                  

                  但是,什么都没有显示.

                  However, nothing was displayed.

                  推荐答案

                  这种代码注入最好使用 简码.而不是 document.createElement,只需使用 wp_enqueue_script 加载 Iframe API,脚本的其余部分由短代码打印.PHP heredoc 可以轻松构建大型可配置 HTML 字符串.

                  This kind of code injection is better done with a Shortcode. Instead of document.createElement, just use wp_enqueue_script to load the Iframe API and the rest of the script is printed by the shortcode. PHP heredoc makes easy to build large configurable HTML strings.

                  帖子/页面内的短代码用法为:[ytplayer id="M7lc1UVf-VE"]

                  Shortcode usage inside the post/page would be: [ytplayer id="M7lc1UVf-VE"]

                  add_shortcode( 'ytplayer', 'print_yt_player' );
                  
                  function print_yt_player( $atts ) {
                      wp_enqueue_script( 'yt-iframe', 'https://www.youtube.com/iframe_api' );
                      $yt_id = $atts['id'];
                      $html = <<<HTML
                          <div id="player"></div>   
                          <script>
                              var player, done = false;
                              function onYouTubeIframeAPIReady() {
                                  player = new YT.Player('player', {
                                      height: '390',
                                      width: '640',
                                      videoId: '$yt_id',
                                      playerVars: { 'autoplay': 1, 'controls': 0 },
                                      events: {
                                          'onReady': onPlayerReady,
                                          'onStateChange': onPlayerStateChange
                                      }
                                  });
                              }    
                              function onPlayerReady(event) {
                                  event.target.playVideo();
                              }
                              function onPlayerStateChange(event) {
                                  if (event.data == YT.PlayerState.PLAYING && !done) {
                                      setTimeout(stopVideo, 6000);
                                      done = true;
                                  }
                              }
                              function playVideo() {
                                  player.playVideo();
                              }
                              function pauseVideo() {
                                  player.pauseVideo();
                              }
                              function stopVideo() {
                                  player.stopVideo();
                              }
                              function loadVideoById(val) {
                                  player.loadVideoById(val, 0, "large");
                              }
                          </script>
                  HTML;
                      return $html;
                  }
                  

                  这篇关于YouTube iframe API 和 WordPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

                    <small id='kodP3'></small><noframes id='kodP3'>

                      <tbody id='kodP3'></tbody>
                    <i id='kodP3'><tr id='kodP3'><dt id='kodP3'><q id='kodP3'><span id='kodP3'><b id='kodP3'><form id='kodP3'><ins id='kodP3'></ins><ul id='kodP3'></ul><sub id='kodP3'></sub></form><legend id='kodP3'></legend><bdo id='kodP3'><pre id='kodP3'><center id='kodP3'></center></pre></bdo></b><th id='kodP3'></th></span></q></dt></tr></i><div id='kodP3'><tfoot id='kodP3'></tfoot><dl id='kodP3'><fieldset id='kodP3'></fieldset></dl></div>

                        • <bdo id='kodP3'></bdo><ul id='kodP3'></ul>

                            <legend id='kodP3'><style id='kodP3'><dir id='kodP3'><q id='kodP3'></q></dir></style></legend>
                          1. <tfoot id='kodP3'></tfoot>