0

Why do I get the PHP warning "no suitable wrapper" for file_get_contents in this shortcode? It's probably more a PHP issue than WordPress.

The shortcode works fine and outputs in a page/post. But my error log is filling up with the warnings. I know PHP warnings are not serious, but I still want to fix the code that throws the warning.

// Wikipedia Last Edit Date

function wikipedia_article_date() {

$url = "https://en.wikipedia.org/w/api.php?action=query&titles=Test_article&prop=revisions&rvlimit=1&format=json";

$data = json_decode(file_get_contents($url), true);
$date = $data['query']['pages']['746140638']['revisions'][0]['timestamp'];

$date = new DateTime($date);
return $date->format('m-d-Y'); 
}

add_shortcode('article_date','wikipedia_article_date');
2

1 Answer 1

1

Use the HTTP API:

$http = wp_remote_get( 'https://en.wikipedia.org/w/api.php?action=query&titles=Test_article&prop=revisions&rvlimit=1&format=json' );

if ( ! $body = wp_remote_retrieve_body( $http ) )
    return;

if ( ! $data = json_decode( $body, true ) )
    return;

$date = new DateTime( $data['query']['pages']['746140638']['revisions'][0]['timestamp'] );

return $date->format( 'm-d-Y' ); 
1
  • Thanks! Works great, with no need for allow_url_fopen or allow_url_include. Commented Dec 4, 2016 at 16:45

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.