admin管理员组

文章数量:1122846

So I have a basic json file set up with some basic objects in it. My plugin is built to retrieve the data from the file (in the same directory as the main php file), and use it for different purposes, though right now I'm just trying to verify that it's getting the data. When file_get_contents is called from a filter hook, everything returns and runs as it should. When it's called from an action hook, however, it returns { "documents": [null] } from the file. Where there is null in this return, there's actually a great deal of data in the file, but for some reason it's not accessing it. I don't think it's anything to do with my code, as I've verified that my line to call for the data is identical in all places. I can't think of what else could be causing this problem. I also tried fopen. Same result. It's not properly reading the file.

Hooks:

    add_action('wp_ajax_search_contact_files', array($this, 'search_files_handler');
    add_action('wp_ajax_nopriv_search_contact_files', array($this, 'search_files_handler');

The handler is fairly simply, just retrieving a bit of data being passed, then calling my search method. All that does right now is:

    $jsonString = file_get_contents('dummy_data.json', true);
    return $jsonString;

So I have a basic json file set up with some basic objects in it. My plugin is built to retrieve the data from the file (in the same directory as the main php file), and use it for different purposes, though right now I'm just trying to verify that it's getting the data. When file_get_contents is called from a filter hook, everything returns and runs as it should. When it's called from an action hook, however, it returns { "documents": [null] } from the file. Where there is null in this return, there's actually a great deal of data in the file, but for some reason it's not accessing it. I don't think it's anything to do with my code, as I've verified that my line to call for the data is identical in all places. I can't think of what else could be causing this problem. I also tried fopen. Same result. It's not properly reading the file.

Hooks:

    add_action('wp_ajax_search_contact_files', array($this, 'search_files_handler');
    add_action('wp_ajax_nopriv_search_contact_files', array($this, 'search_files_handler');

The handler is fairly simply, just retrieving a bit of data being passed, then calling my search method. All that does right now is:

    $jsonString = file_get_contents('dummy_data.json', true);
    return $jsonString;
Share Improve this question edited Jul 10, 2019 at 17:18 CommunityBot 1 asked Jul 10, 2019 at 13:44 SaeraleisSaeraleis 1 2
  • 2 What are the hooks? Can you share the code? – Jacob Peattie Commented Jul 10, 2019 at 14:21
  • Have you considered using the REST API instead and add your own endpoint? – Tom J Nowell Commented Jul 10, 2019 at 18:35
Add a comment  | 

1 Answer 1

Reset to default 0

If I understand you correctly, your dummy_data.json file is placed in the root directory of your page (the same directory where index.php and wp-config.php are placed), right?

In that case you can easily access that file passing this path: 'dummy_data.json' only when your code is run from index.php file.

But AJAX calls are fun from wp-admin/admin-ajax.php file. So when you try to open 'dummy_data.json' during ajax action, your code will try to open file called dummy_data.json placed in wp-admin directory - and there is no such file.

You can solve it by adding ABSPATH to the path:

get_file_contents( ABSPATH . '/dummy_data.json' );

本文标签: phpfilegetcontents returning different in filters and actions