使用_Content筛选器将音频链接替换为jplay

时间:2013-07-06 作者:Vigs

我正在尝试使用:

add_filter( \'the_content\', \'filter_function_name\' );
在我的帖子中找到链接到的所有锚定标签。mp3文件,然后用一个jplayer实例来替换它们,该实例将播放该文件,而不仅仅是显示要作为文件的链接。

因此,我试图将由\\u内容输出的所有锚标记替换为以下html和js的实例:

html:

  <div id="jquery_jplayer_#uniqueid" class="jp-jplayer"></div>
  <div id="jp_container_#uniqueid" class="jp-audio">
    <div class="jp-type-single">
      <div class="jp-gui jp-interface">
        <ul class="jp-controls">
          <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
          <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
          <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
          <li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>
          <li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>
          <li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>
        </ul>
        <div class="jp-progress">
          <div class="jp-seek-bar">
            <div class="jp-play-bar"></div>
          </div>
        </div>
        <div class="jp-volume-bar">
          <div class="jp-volume-bar-value"></div>
        </div>
        <div class="jp-time-holder">
          <div class="jp-current-time"></div>
          <div class="jp-duration"></div>
          <ul class="jp-toggles">
            <li><a href="javascript:;" class="jp-repeat" tabindex="1" title="repeat">repeat</a></li>
            <li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="repeat off">repeat off</a></li>
          </ul>
        </div>
      </div>
      <div class="jp-title">
        <ul>
          <li>Music Player</li>
        </ul>
      </div>
      <div class="jp-no-solution">
        <span>Update Required</span>
        To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>.
      </div>
    </div>
  </div>
Javascript:

<script type="text/javascript">
$(document).ready(function(){
  $("#jquery_jplayer_#uniqueid").jPlayer({
    ready: function () {
      $(this).jPlayer("setMedia", {
        m4a: "#the_url_from_the_anchor_tag",
      }) //.jPlayer("play");
    },
    swfPath: "js/jplayer",
    supplied: "m4a, mp3, oga",
    wmode:"window",
    cssSelectorAncestor: \'#jp_container_#uniqueid\'
  });
  });
 </script>
非常感谢您的帮助。谢谢

2 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

您需要在the_content 并更改标记。这应替换的标记<a> URL以结尾的标记.mp3, 你在评论中特别提到了这一点。

function replace_audio_link_cb_105555($match) {
  if (!empty($match[1]) && \'.mp3\' == substr($match[1],-4)) {
    $your_long_block_of_markup = \'<div id="jquery_jplayer_#uniqueid" class="jp-jplayer"></div>
    <div id="jp_container_#uniqueid" class="jp-audio">... and so on ...</div>\';
    $your_javascript = \'<script type="text/javascript">
      jQuery(document).ready(function(){
        jQuery("#jquery_jplayer_#uniqueid").jPlayer({
          ready: function () {
            jQuery(this).jPlayer("setMedia", {
              m4a: "#the_url_from_the_anchor_tag",
            }) //.jPlayer("play");
          },
          swfPath: "js/jplayer",
          supplied: "m4a, mp3, oga",
          wmode:"window",
          cssSelectorAncestor: "\'.$match[1].\'"
        });
        });
      </script>\';
    $match[0] = $your_long_block_of_markup.$your_javascript;
  }
  return $match[0];
}

function replace_audio_link_105555($content) {
  $pattern = \'|<a.*?href="(.*)".*>?.*?(?:</a>)?|\';
  $content = preg_replace_callback($pattern,\'replace_audio_link_cb_105555\',$content);
  return $content;
}
add_filter(\'the_content\',\'replace_audio_link_105555\'); 
如果是我,我会想办法让javascript更加灵活,这样我就可以注册并排队,而不是为每个音频文件单独打印。

SO网友:Gino

我这样做是为了找到所有音频、视频标签和mp3、m4v链接,并在jPlayer中转换主题http://jsfiddle.net/onigetoc/vaot2auz/

结束

相关推荐

如何访问调用Apply_Filters()的函数中的变量?

我试图根据调用的函数中的变量是否apply_filters() 等于特定值。该变量不会传递给apply_filters() 参数。这或许可以解释我的意思:// function in wordpress core function get_var_b() { // generating $var_a $var_a = \"a\"; // some code ... $var_b = apply_filters(\'get_var_b\