php - How to limit the content coming from wordpress shortcodes?

I am working on a wordpress shortcodes in which I want to the limit the content coming from a xml.The code which I have

I am working on a wordpress shortcodes in which I want to the limit the content coming from a xml.

The code which I have used for wordpress shortcodes is:

function podcast_func( $content = null ){
    ob_start();
    ?>
<script src=".js"></script>
<center><div id="podcast" align="center"></div></center> 
<script> 
var PodcastplayerInstance = jwplayer("podcast"); 
PodcastplayerInstance.setup({ 
  playlist: ".xml", 
  androidhls: true, 
  preload: "auto", 
  height: 200, 
  width: 400,
  visualplaylist:false,
  stretching: "fill",
    "plugins": {
        ".js":{},
        'viral-2': {'oncomplete':'False','onpause':'False','functions':'All'}
    }
});
</script> 
<?PHP
    return ob_get_clean();
}
add_shortcode( 'podcast', 'podcast_func' );

On using this <div class="today-podcast" style="text-align: center;">[podcast]</div>, it displays the entire content from here .xml

Problem Statement: I am wondering what changes I should make in the wordpress shortcode above so that it displays only 1st two items from here .xml

I am working on a wordpress shortcodes in which I want to the limit the content coming from a xml.

The code which I have used for wordpress shortcodes is:

function podcast_func( $content = null ){
    ob_start();
    ?>
<script src="https://content.jwplatform/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' );

On using this <div class="today-podcast" style="text-align: center;">[podcast]</div>, it displays the entire content from here http://www.cpac.ca/tip-podcast/jwplayer.xml

Problem Statement: I am wondering what changes I should make in the wordpress shortcode above so that it displays only 1st two items from here http://www.cpac.ca/tip-podcast/jwplayer.xml

Share Improve this question asked Mar 31, 2019 at 23:41 user5447339user5447339 819 bronze badges 2
  • That's an external xml file. You will need to parse the xml file and instead of sending the entire unfiltered xml file URL as the value into the jwplayer, you can pass the parsed playlist as a JSON object like this: developer.jwplayer/jw-player/docs/developer-guide/… – Samuel Liew Commented Apr 1, 2019 at 0:01
  • 1 @SamuelLiew Thanks for the comment. I am wondering if you can let me know in an answer what I have to do. – user5447339 Commented Apr 1, 2019 at 0:03
Add a comment  | 

2 Answers 2

Reset to default 1 +50

It's better to 1st save the returned XML to a file and then loop back to 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/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> 

In case you want the 2nd or 3rd element only, update the above code with the following

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]);
}

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745622787a4636622.html

相关推荐

  • php - How to limit the content coming from wordpress shortcodes?

    I am working on a wordpress shortcodes in which I want to the limit the content coming from a xml.The code which I have

    10小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信