【问题标题】:Access a local JSON file in Wordpress Theme在 Wordpress 主题中访问本地 JSON 文件
【发布时间】:2017-09-05 19:37:22
【问题描述】:

背景 当用户在 Wordpress 后端保存页面时,我正在动态创建一个 JSON 数据文件。我正在通过钩子“save_post”执行此操作,并使用 file_put_contents 将文件“network.json”保存到我的主题文件夹的根目录中。我这样做是为了可以从我的主题中的 js 脚本访问特定数据。

目前的方法 我的主题中有一个 js 文件,其中包含以下 JS。以下是有效的,但我想知道这是否是在 WP 主题中调用本地 JSON 文件的最佳方法。

$.getJSON( "../wp-content/themes/ihdf/network.json", function(data) {
    console.log(data);
});

上述方法是正确且技术上最合理的方法吗?

其他方法

我以前在 Wordpress 中使用过 ajax,方法是将脚本加入队列并设置适当的 ajax 函数以使用 admin-ajax.php 调用。对于我的需求来说,这似乎过于复杂。

我还可以在我的模板文件中设置一个 js 变量,如下所示:

var networkJSON = <?php get_template_directory_uri() . '/network.json' ?>

【问题讨论】:

  • 本地化脚本就是这样做的方法!请参阅文档here。您可以使用其他 js 文件可以访问的本地化数据将脚本排入队列。答案here是我正在采取的方法。

标签: php json ajax wordpress


【解决方案1】:

在服务器端处理远程请求时,file_get_contents() 函数似乎是一个可靠的选择,但 WordPress 已经包含一个非常有用的 API,称为 HTTP API

HTTP API 可用于向远程 API 发送数据和从远程 API 检索数据,这也意味着对您自己的服务器的任何请求。

WordPress 中的 HTTP API 有四个主要功能:

例如,您可以使用 wp_remote_get()network.json 文件中检索数据,然后将其与 wp_localize_script() 一起解析strong> 函数,将您需要的数据暴露给入队的 js 文件。

请参考以下功能(未经测试),但您应该没有任何问题。

-- 函数--

function wp_request_localize_my_json_data() {

    // Helpers to define the $url path
    //$protocol = is_ssl() ? 'https' : 'http';
    $directory = trailingslashit( get_template_directory_uri() );

    // Define the URL
    $url = $directory . 'network.json';

    // Register main js file to be enqueued
    wp_register_script( 'network-js', $directory . 'assets/js/network.js', array('jquery'), false, true  );

    // Make the request
    $request = wp_remote_get( $url );

    // If the remote request fails, wp_remote_get() will return a WP_Error, so let’s check if the $request variable is an error:
    if( is_wp_error( $request ) ) {
        return false; // Bail early
    }

    // Retrieve the data
    $body = wp_remote_retrieve_body( $request );
    $data = json_decode( $body );

    // Localize script exposing $data contents
    wp_localize_script( 'network-js', 'networkJSON', array( 
            'network_url' => admin_url( 'admin-ajax.php' ),
            'full_data' => $data
        )
    );

   // Enqueues main js file
    wp_enqueue_script( 'network-js' );

}
add_action( 'wp_enqueue_scripts', 'wp_request_localize_my_json_data', 10);

如果一切顺利,您最终可能会得到从 network.json 文件中检索到的本地化数据供您使用。

现在假设您在 network.json 文件中有一个名为 current_user 的变量。因此,为了在排队的 JS 文件中访问此变量,您只需执行以下操作:

<script type="text/javascript">
    var my_data = networkJSON.full_data;
    var user = my_data.current_user;
</script>

【讨论】:

  • 感谢@adriano 很棒的方法。考虑到我在自己的服务器中工作,我没有考虑使用 HTTP API,但这是有道理的。一个问题是如何避免在这里使用绝对 URL $url = ''. $protocol . '://example.com/wp-content/themes/ihdf/network.json';
  • 您好@Celso - 抱歉,$url 变量是$url = $directory . 'network.json'; - 假设文件存在于主题的主目录中。您也可以将其更改为任何其他路径,例如 $url = $directory . 'dynamic_data/network/network.json'; - 这反过来又假设这些目录也存在于您的主题的主目录中。我已经更新了代码以反映它。
【解决方案2】:

如果您想通过插件或主题在wordpress中读取json文件,请使用此功能。

function get_local_file_contents( $file_path ) {
    ob_start();
    include $file_path;
    $contents = ob_get_clean();

    return $contents;
}

今晚我想在插件中我的php文件旁边的json文件夹中读取一个json文件并找到这个article

我不能用 file_get_contents()

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2011-07-07
    • 1970-01-01
    • 2015-11-12
    相关资源
    最近更新 更多