我想在我的主题functions.php 可以在服务器上显示CSV文件内容的文件。
稍后我会担心将CSV文件读入数组等,但现在我只想确保我可以使用file_get_contents. 但是,当我运行下面的短代码时,警报包含HTML。
function display_csv_data_func( $atts ) {
$file_contents = file_get_contents("http://example.com/something/file.csv");
return "<script>alert(\'" . $file_contents . "\');</script>";
}
add_shortcode( \'display_csv_data\', \'display_csv_data_func\' );
返回值:
alert(\'
<!doctype html>
<!--[if lt IE 7 ]> <html class="no-js ie6" lang="en-US"
...
是否有关于短代码和函数的内容。php或WordPress通常会干扰我的代码?看起来这应该是一件相当简单的事情。。。
最合适的回答,由SO网友:jeffbeene 整理而成
啊哈,我想出来了!你必须使用wp_remote_get(). 我现在不太清楚为什么,但代码是这样的:
function display_csv_data_func( $atts ) {
$file_path = "http://example.com/something/file.csv";
$response = wp_remote_get($file_path);
$response_body = wp_remote_retrieve_body($response);
return "<script>alert(\'" . $response_body . "\');</script>";
}
add_shortcode( \'display_csv_data\', \'display_csv_data_func\' );