0

I'm using an xml file to gather informations about my employees (hours of work and such) and I want to show these infos in a page on the website of the company based on the employee logged in. To do so I thought I could create a shortcode, here is the code I'm using:

function register_xml_file() {
    wp_register_style( 'porva', get_stylesheet_directory_uri() .  '/porva.xml'  );
    wp_enqueue_style( 'porva' );
}
add_action( 'wp_enqueue_scripts', 'register_xml_file' );

function dati_tbcrew($atts){
    global $user_login;
    get_currentuserinfo();
    $percorso=get_stylesheet_directory_uri();
    $percorso.="/porva.xml";
    $xml=simplexml_load_file($percorso) or die("Error: Cannot create object");
    switch($xml->name['nome']){
        case $user_login: $out=$xml->name->contenuto;break;
        default:break;
    }
    return $out;
}
add_shortcode('dati_tbcrew', 'dati_tbcrew');

Unfortunately the output is:

Error: Cannot create object

Why can't simplexml_load_file find the xml file?

Febz
  • 39
  • 7
  • Why are you trying to register it as a Style? – Musk Apr 27 '15 at 15:17
  • *"What am I missing?"* - as you've written the code and this is the *dying* error message, you're probably missing, why you've put the `die` in there. So if you could elaborate why you actually placed the *`die`* in there this could shed some light on your question but so far you're hiding that information. If you can't than this already answers your question: You're missing that you bring the software to a halt in case [`simplexml_load_file`](http://php.net/simplexml_load_file) returns a value that equals boolean FALSE (in PHP slang: is falsy). – hakre Apr 28 '15 at 06:31
  • I'm not missing why I'm using the _die_, you're missing that (and I don't get why). I'm using it to signal that simplexml_load_file is not finding the xml file, ergo returning FALSE. I'll change the question so you won't get confused anymore. – Febz Apr 28 '15 at 11:27
  • *Die* in this case could mean something else other than not finding the file, it could be permission related or simplexml extension related. Since you're code cannot provide a more descriptive response we are left guessing. – Musk Apr 28 '15 at 12:38
  • *"Why can't simplexml_load_file find the xml file?"* - simplexml_load_file returning false must not meant that the file is not found. There can be plenty of reasons why the operation fails. Next to not finding the file, this can be wrong content within the file. So dying at that point actually is not that bad at all - you comment it as well - it's just it wasn't clear to me about which part you were concerned and what your expectations were. I suggest you turn on error display and logging to the highest level to learn more about the cause of the problem. – hakre Apr 28 '15 at 20:10
  • More information on error message in PHP and how to track them / enable logging is here: [How to get useful error messages in PHP?](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – hakre Apr 28 '15 at 20:11
  • Ok, I get it now, I managed to look at the error given and the problem is in the syntax I used to build the test xml file (here is the error `Fatal Error 5: Extra content at the end of the document Line: 5 Column: 3`) . Thank you all for the help and patience guys!!! As it turns out I forgot the root tag lol. – Febz Apr 29 '15 at 15:14

1 Answers1

0

Something like this alone should work, also consider using a different path, will edit this answer once more information is provided. Like the XML structure and location where this file will be taken from.

function employee_info_func(){

    $user = wp_get_current_user();

    if(/** Validate User **/ FALSE){
        return;
    }else{
        $path_to_file = '';

        $xml = $xml=simplexml_load_file($path_to_file);

        return 'Print Whatever';
    }

}

add_shortcode('employee_info','employee_info_func');

A better practice would probably upload the XML information into a json object on the Database and pull that information.

Musk
  • 1,477
  • 1
  • 15
  • 25
  • I tried your code and removed the registration but unfortunately nothing changed. I'll try with json. – Febz Apr 27 '15 at 19:53
  • Your path is most likely wrong, which path you trying to get the xml from? – Musk Apr 27 '15 at 19:54
  • The xml file is in my child-theme folder, I tried: - $path=get_stylesheet_directory() . "/porva.xml"; - $path=get_stylesheet_directory_uri() . "/porva.xml"; - $path="/web/wp-content/themes/canvas-child/porva.xml"; - $path="/wp-content/themes/canvas-child/porva.xml"; none of which worked. – Febz Apr 27 '15 at 21:16
  • You should pass that into a function such as file_exists() to validate that what you are trying to get is actually valid. Also if it is valid but you can't open it you might want to check the permission to that file. – Musk Apr 28 '15 at 12:35
  • With file_exists() I managed to find the right path to use, thank you! I changed the file's permissions too but it's still not working. – Febz Apr 28 '15 at 12:46
  • 1
    Try fetching the error generated see doc here http://php.net/manual/en/function.libxml-get-errors.php – Musk Apr 28 '15 at 12:52