Skip to content

Instantly share code, notes, and snippets.

@sanzeeb3
Last active March 27, 2020 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanzeeb3/f772810c40a837a6ec6d1ed5791d9b7e to your computer and use it in GitHub Desktop.
Save sanzeeb3/f772810c40a837a6ec6d1ed5791d9b7e to your computer and use it in GitHub Desktop.
Display covid-19 data with the shortcode.
/**
* Display covid-19 data with the shortcode.
*
* Usage [covid19_data country="all"]
*
* @see http://sanjeebaryal.com.np/display-covid-19-data-in-your-site-without-plugin/.
*
* @param array $atts Attributes to the shortcode.
*
* @return string.
*/
function covid19_data( $atts ) {
$country = ! isset( $atts['country'] ) || $atts['country'] === 'all' ? '' : $atts['country'];
$api_call = 'https://corona.lmao.ninja/countries/'.$country;
$data = wp_remote_get( $api_call );
$code = wp_remote_retrieve_response_code( $data );
$body = wp_remote_retrieve_body( $data );
$result = json_decode( $body );
ob_start();
echo '<table class="covid19-data-entries">';
echo '<thead><tr>';
echo '<td>Country</td>';
echo '<td>Cases</td>';
echo '<td>Today Cases</td>';
echo '<td>Deaths</td>';
echo '<td>Today Deaths</td>';
echo '<td>Recovered</td>';
echo '<td>Active</td>';
echo '<td>Critical</td>';
echo '<td>Cases Per One Million</td>';
echo '<td>Deaths Per One Million</td>';
echo '</tr></thead>';
if ( ! empty( $country ) ) {
$result = array( $result );
}
foreach( $result as $res ) {
echo '<tbody><tr>';
echo '<td>'. $res->country . '</td>';
echo '<td>'. $res->cases . '</td>';
echo '<td>'. $res->todayCases . '</td>';
echo '<td>'. $res->deaths . '</td>';
echo '<td>'. $res->todayDeaths . '</td>';
echo '<td>'. $res->recovered . '</td>';
echo '<td>'. $res->active . '</td>';
echo '<td>'. $res->critical . '</td>';
echo '<td>'. $res->casesPerOneMillion . '</td>';
echo '<td>'. $res->deathsPerOneMillion . '</td>';
echo '</tr></tbody>';
}
echo '</table>';
return ob_get_clean();
}
add_shortcode( 'covid19_data', 'covid19_data');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment