如何在Wordpress页面中显示自定义xml文件的内容?

时间:2021-11-18 01:15:40

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:

我正在使用xml文件来收集有关我的员工的信息(工作时间等),我想根据员工的登录情况在公司网站的页面中显示这些信息。为此我认为我可以创建一个短代码,这是我正在使用的代码:

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?

为什么simplexml_load_file找不到xml文件?

1 个解决方案

#1


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.

单独这样的东西应该工作,也考虑使用不同的路径,将提供更多信息后编辑此答案。就像XML结构和从中获取此文件的位置一样。

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.

更好的做法可能是将XML信息上传到数据库中的json对象并提取该信息。

#1


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.

单独这样的东西应该工作,也考虑使用不同的路径,将提供更多信息后编辑此答案。就像XML结构和从中获取此文件的位置一样。

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.

更好的做法可能是将XML信息上传到数据库中的json对象并提取该信息。