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 ;)
My Answer:
>request->get( 'url' ) );_ $xml = new XMLReader();_ $xml->open( $url );_ while( $xml->read() ) {_ if( $xml->nodeType == XMLReader::ELEMENT && $xml->localName == 'phone_number' ) {_ $xml->read();_ $result = $xml->value;_ break;_ }_ }_ $xml->close();_ return $result;_}_// Usage example:_$phone_number = Get_Page_Phone_Number();_echo $phone_number;_I have also tried using SimpleXML method but it also fails to read the xml file after the wordpress update. Any idea why this might be happening and how to fix it?
Rate this post
4 of 5 based on 4011 votesComments