1

I was using a solution for reading an xml file and returning a value from one of its nodes using XMLreader and SimpleXML method provided by Josh Davis in the following thread: How to use XMLReader in PHP?

A few days ago wordpress updated to 5.9.3 and I think this is why this method has stopped returning that corresponding value and now returns the default value instead. here's my code (that seemed to work fine for a long time untill this 5.9.3 version got pushed): (blurred domain name and other details).

function Get_Page_Phone_Number() {
    static $result;
    // Checks if Function has already run
    if ( $result !== null )
        return $result; //Outputs Phone Number Result instead of running again
    // Lot of work here to determine $result
    global $wp;
    global $lastSegment;
    $url = add_query_arg( array(), $wp->request );
    $lastSegment = basename(parse_url($url, PHP_URL_PATH));    //this is Current Page Slug
    $z = new XMLReader;
    $xmlurl = get_site_url();
    $z->open( 'https://www.****.com/PhonesLP/Pnumbers23.xml');
    $doc = new DOMDocument;
                            global $phone;
                            global $freenote;
                            $phone = '(xxx) xxx-xxxx';//default num if node not found
    // move to the first <page /> node
    while ($z->read() && $z->name !== 'page');
                    // now that we're at the right depth, hop to the next <page/> until the end of the tree                 
                    while ($z->name === 'page')
                    {
                        // either one should work
                        //$node = new SimpleXMLElement($z->readOuterXML());
                        $node = simplexml_import_dom($doc->importNode($z->expand(), true));
                    if ($node->name == $lastSegment){
                        $phone = $node->num;
                        $freenote = '24/7 Local Number';
                        break;
                        }
                    $z->next('page');               
                    }
                        $result = $phone;
                        return $result;
    $xml->close();
}
add_shortcode( 'Get_Phone_Number', 'Get_Page_Phone_Number' );
  • the function is checking the current visited page url slug, matching it to the xml file node: "name" and if matches, saves the current pages node "num" into $phone. Then returning $phone. looping all pages in the xml. so when the function is called it returns the current pages phone number according to the current pages slug compared to the xml file built like this for example:

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <pages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <page>
          <name>page-slug-url</name>
          <num>(123) 456-7891</num>
      </page>
      <page>
          <name>page-slug-url2</name>
          <num>(123) 456-7890</num>
      </page>
      </pages>
    

if node name not found it returns the default number set before the while loop.

Please help figure why is it always returning the default number after wordpress got updated to 5.9.3? edit: checked php version and it was on 7.2, tried downgrading to 7.1, then upgrading to 7.4, nothing seems to help.

Thanks ;)

1 Answers1

1

Found my own answer..

the xml file was not loaded at all, only after I removed the www from the path it loaded well and the right value returned, While this seemed to work fine before this wordpress update even with the www in the path.

so:

$z->open( 'https://www.****.com/data.xml'); // Not working 
after wordpress 5.9.3 update.

$z->open( 'https://****.com/data.xml');    // This seemed to 
work - just remove the www or use some get_site_url(); in some other way.