I am trying to get data from JSON file in Wordpress template using file_get_contents But can not get the file

$data = file_get_contents("../assets/data2.json");
$arr = json_decode($data, true);

Facing this error while var_dump $data

object(WP_Error)#825 (2) { ["errors"]=> array(1) { ["http_request_failed"]=> array(1) { [0]=> string(29) "A valid URL was not provided." } } ["error_data"]=> array(0) { } }

My data2.json file is in the assets folder and trying from templates folder in theme folder in wordpress.

1 Answer 1


file_get_contents() is usually blocked to fetching content from URLs for safety reasons. You should provide a local server path. In alternative use wp_remote_get()

  • I already use wp_remote_get() but can't fix my problem.
    – Shawn
    Apr 19, 2020 at 15:51
  • you have to specify a full url like example.com/your-api
    – Patareco
    Apr 19, 2020 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.