A Better Way to Add a PHP page to my Site

Discussion in 'WordPress' started by MiletNZ, Sep 23, 2010.

  1. #1
    I have a PHP script that someone made me to pull in RSS feeds from another site and the only way I could think of getting it into my website and to look like all my other pages was to make a template for it.

    But the thing is I need to re-make this script about 40 times - there is about 40 different feeds I need and they all need to be on their own page. So I'm going to end up having HEAPS of different templates and that is a bit messy so I need to find another way to do it.

    This is the code that Kerosene (thanks again) helped me with and I basically need to do it over and over again except changing the RSS feed with a different location each time - it is for weather feeds.

    <?php
    function My_simplexml_load_file($URL)
    {
    $ch = curl_init($URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $xml = simplexml_load_string(curl_exec($ch));
    curl_close($ch);
    return $xml;
    }
    ?>
    
    <?php
    $url='http://www.weathermap.co.nz/feed/boating/kaikoura-peninsula';
    $xml = My_simplexml_load_file($url);
    if ($xml!=''){
    foreach ($xml->channel->item as $item){
    $description = $item->description;
    echo $description;
    }
    }
    ?>
    
    Code (markup):
    I am by no means a Programmer and this is my first time playing with these sorts of scripts so I'm a bit lost and asking for help everyday.
     
    Last edited: Sep 23, 2010
    MiletNZ, Sep 23, 2010 IP
  2. juhasan

    juhasan Well-Known Member

    Messages:
    389
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    125
    #2
    Modify (add PHP code) your custom page template and save it with different name using a Text Editor. Then login to WordPress, create a new page and choose that custom page template.

    Hope this works!
     
    juhasan, Sep 23, 2010 IP
  3. MiletNZ

    MiletNZ Member

    Messages:
    283
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #3
    Thanks for your help but I don't think I explained my situation properly, sorry. I have been doing it this way, but the list of templates has started to get massive and I wanted to know if there was another way I could do it.

    Still thanks for your input though.
     
    MiletNZ, Sep 23, 2010 IP
  4. MiletNZ

    MiletNZ Member

    Messages:
    283
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #4
    Just to add to this, after going through and seeing how many RSS feeds I have, its 299 RSS feeds.............
     
    Last edited: Sep 23, 2010
    MiletNZ, Sep 23, 2010 IP
  5. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #5
    Why wouldn't you just use one custom wordpress page template and set a custom field for something like feedurl and then pass custom field to the above function?

    add to your code :
    $feedurl = "WHATEVER_YOU_NAME_THE_CUSTOM_FIELD";
    $url = get_post_meta($post_id, $feedurl, $single);
    if(!$url)
    {
    echo "<strong> Feed Unavailable </strong>";
    }
    else
    {
    $xml = My_simplexml_load_file($url);
    if ($xml!=''){
    foreach ($xml->channel->item as $item){
    $description = $item->description;
    echo $description;
    }
    }
    }
    ?>

    This way, whatever the page/post title is could be used as well.
     
    shallowink, Sep 24, 2010 IP
  6. MiletNZ

    MiletNZ Member

    Messages:
    283
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #6
    Thanks for helping, but I can't seem to get that working
     
    MiletNZ, Sep 25, 2010 IP
  7. thedark

    thedark Well-Known Member

    Messages:
    1,346
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    168
    Digital Goods:
    1
    #7
    You want one page where to show the feeds but you don't want to re-create the template for that page. shallowink told you how to do it and he is right, but, you first have to create the page where you want to display that. Create it and get his ID ( a number ),you can see in the url when you edit it.

    Then open your page.php file, and where the_content() is called, type after if($post->id=='YOUR_PAGE_ID') { PASTE HERE YOUR CODE }

    That's it , change YOUR_PAGE_ID with the id of your page.
     
    thedark, Sep 25, 2010 IP
  8. vrktech

    vrktech Well-Known Member

    Messages:
    449
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #8
    I think this will work

    You have 40 urls,

    Create a page template with the code
    <?php
    function My_simplexml_load_file($URL)
    {
    $ch = curl_init($URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $xml = simplexml_load_string(curl_exec($ch));
    curl_close($ch);
    return $xml;
    }
    ?>
    
    <?php
    $url = get_post_meta($post_id, 'YOUR_CUSTOM_FIELD', $single);
    if(!$url)
    {
    echo "<strong> Feed Unavailable </strong>";
    }
    else
    {
    $xml = My_simplexml_load_file($url);
    if ($xml!=''){
    foreach ($xml->channel->item as $item){
    $description = $item->description;
    echo $description;
    }
    }
    }
    ?>
    
    Code (markup):
    Now you will create 40 pages selecting the same page template, each post will have corresponding feed url in the custom field with name YOUR_CUSTOM_FIELD

    This way you will not have many page templates on your server
     
    vrktech, Sep 25, 2010 IP
  9. MiletNZ

    MiletNZ Member

    Messages:
    283
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #9
    Cheers for the help, have put this on hold for now as I've got too much other stuff to do.
     
    MiletNZ, Oct 4, 2010 IP