如何限制来自WordPress快捷码的内容?

时间:2019-03-31 作者:user5447339

我正在开发一个wordpress短代码,我想限制来自xml的内容。

我用于wordpress短代码的代码是:

function podcast_func( $content = null ){
    ob_start();
    ?>
<script src="https://content.jwplatform.com/libraries/FZ8yNTef.js"></script>
<center><div id="podcast" align="center"></div></center> 
<script> 
var PodcastplayerInstance = jwplayer("podcast"); 
PodcastplayerInstance.setup({ 
  playlist: "http://www.cpac.ca/tip-podcast/jwplayer.xml", 
  androidhls: true, 
  preload: "auto", 
  height: 200, 
  width: 400,
  visualplaylist:false,
  stretching: "fill",
    "plugins": {
        "http://www.cpac.ca/tip-podcast/listy.js":{},
        \'viral-2\': {\'oncomplete\':\'False\',\'onpause\':\'False\',\'functions\':\'All\'}
    }
});
</script> 
<?PHP
    return ob_get_clean();
}
add_shortcode( \'podcast\', \'podcast_func\' );
关于使用此<div class="today-podcast" style="text-align: center;">[podcast]</div>, 它显示此处的全部内容http://www.cpac.ca/tip-podcast/jwplayer.xml

Problem Statement: 我想知道我应该在上面的wordpress快捷码中做些什么更改,以便它只显示从这里开始的前两项http://www.cpac.ca/tip-podcast/jwplayer.xml

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

最好先将返回的XML保存到文件中,然后循环回unset。

<?php
  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => "http://www.cpac.ca/tip-podcast/jwplayer.xml",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => array(
      "cache-control: no-cache",
      "postman-token: 28025ee8-1e82-ce60-f6ae-f401118baa1c"
    ),
  ));

  $response = curl_exec($curl);
  $err = curl_error($curl);

  curl_close($curl);

  if ($err) {
    echo "cURL Error #:" . $err;
  } else {
    $fp = fopen(ABSPATH.\'jwp.xml\', \'w\');
    fwrite($fp, $response);
    fclose($fp);
  }

  $xml = simplexml_load_file(ABSPATH.\'jwp.xml\');

  for($i = count($xml->channel->item); $i >= 2; $i--){
    unset($xml->channel->item[$i]);
  }

  $xml->saveXML(ABSPATH.\'jwp.xml\');

  ?>
  <script src="https://content.jwplatform.com/libraries/FZ8yNTef.js"></script>
  <center><div id="podcast" align="center"></div></center> 
  <script> 
  var PodcastplayerInstance = jwplayer("podcast"); 
  PodcastplayerInstance.setup({ 
    playlist: "<?php echo site_url(); ?>/jwp.xml", 
    androidhls: true, 
    preload: "auto", 
    height: 200, 
    width: 400,
    visualplaylist:false,
    stretching: "fill",
      "plugins": {
          "http://www.cpac.ca/tip-podcast/listy.js":{},
          \'viral-2\': {\'oncomplete\':\'False\',\'onpause\':\'False\',\'functions\':\'All\'}
      }
  });
  </script> 
如果只需要第二个或第三个元素,请使用以下代码更新上述代码

for($i = count($xml->channel->item); $i >= 3; $i--){
  unset($xml->channel->item[$i]);
}

for($i = 0; $i < count($xml->channel->item); $i++){
  unset($xml->channel->item[0]);
}

SO网友:Serkan Algur

You need to parse your XML file for creating a playlist for jwplayer according to this page. But there is a problem. jwplayer:image and jwplayer:source strings cant resolve with simple_xml_load() for parsing.

I create a code for you. Code needs file_get_contens() function enabled by the server (because we must change remote XML and resolve the jwplayer:image problem).

Here is the code;

$source_xml   = \'http://www.cpac.ca/tip-podcast/jwplayer.xml\';
$fileContents = file_get_contents( $source_xml );
$fileContents = str_replace( array( \'jwplayer:image\', \'jwplayer:source\', \' file="\', \'" />\' ), array( \'image\', \'file\', \'>\', \'</file>\' ), $fileContents );
$fileContents = trim( str_replace( \'"\', "\'", $fileContents ) );
$simpleXml    = simplexml_load_string( $fileContents );
$json         = json_encode( array( $simpleXml->channel->item[0], $simpleXml->channel->item[1] ) );

print( $json );

This code resolves your XML file and parses only first two sources for creating playlist items. You can test with your localhost. The result should be like this;

[
  {
    "title": "April 4, 2019",
    "description": "The Prime Minister defends the removal of two former cabinet ministers from the Liberal caucus. Jane Philpott and Jody Wilson-Raybould speak out about the Prime Ministers\' decision. Members of the \\"Daughters of the Vote\\" turn their backs on the Prime Minister, and walk out on Andrew Scheer.",
    "image": "http://media.cpac.ca/_app_images/tip_player_poster.png",
    "file": "http://www.cpac.ca/tip-podcast/1554372812.mp3"
  },
  {
    "title": "April 3, 2019",
    "description": "Jody Wilson-Raybould and Jane Philpott are removed from the Liberal Caucus. Gerald Butts submits text messages, and other evidence, to the justice committee. The Environment Commissioner says Canada isn\'t doing enough to fight climate change. ",
    "image": "http://media.cpac.ca/_app_images/tip_player_poster.png",
    "file": "http://www.cpac.ca/tip-podcast/1554286033.mp3"
  }
]

Best regards

相关推荐

PHP:对REST请求进行身份验证?

我确实使用REST api为正在开发的插件创建了一些自定义端点。它工作得很好,但现在我想保护这些请求:我不需要外部用户(EDIT: 我指的是远程请求),以便能够进行查询。但我只找到有关javascript身份验证的文档(REST API Handbook).如何使用PHP实现身份验证(此处为WP 5.1.1)?谢谢