Skip to content

Instantly share code, notes, and snippets.

@jmarreros
Last active November 24, 2023 00:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jmarreros/a71fc3d2ace7ec7b97ff69611f30c6da to your computer and use it in GitHub Desktop.
Save jmarreros/a71fc3d2ace7ec7b97ff69611f30c6da to your computer and use it in GitHub Desktop.
Muestra datos de una API externa de ejemplo en una página específica de WordPress
<?php //No copiar esta línea
// Filtro para agregar contenido a una página de WordPress
add_filter('the_content', 'dcms_add_custom_content');
// Agregamos contenido sólo a la página con el título "Contenido Vinos"
function dcms_add_custom_content($content){
if ( ! is_page('contenido-vinos') ) return $content;
$html = get_data_api();
return $content.$html;
}
// Función que se encarga de recuperar los datos de la API externa
function get_data_api(){
$url = 'https://api.sampleapis.com/wines/reds';
$response = wp_remote_get($url);
if (is_wp_error($response)) {
error_log("Error: ". $response->get_error_message());
return false;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
$template = '<table class="table-data">
<tr>
<th></th>
<th>Bodega</th>
<th>Vino</th>
<th>Calificacion</th>
<th>País</th>
</tr>
{data}
</table>';
if ( $data ){
$str = '';
foreach ($data as $wine) {
$str .= "<tr>";
$str .= "<td><img src='{$wine->image}' width='20'/></td>";
$str .= "<td>{$wine->winery}</td>";
$str .= "<td>{$wine->wine}</td>";
$str .= "<td>{$wine->rating->average}</td>";
$str .= "<td>{$wine->location}</td>";
$str .= "</tr>";
}
}
$html = str_replace('{data}', $str, $template);
return $html;
}
@xahmonx
Copy link

xahmonx commented Aug 3, 2023

Perfecto funciona al 100% como seia si es con una api key?

@jmarreros
Copy link
Author

Seria obtener el token de autenticación y luego realizar el proceso con ese token, espero hacer un artículo sobre eso próximamente.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment