— source: https://wp-tutorials.tech/add-functionality/access-a-rest-api-from-wordpress/
<?php // If this file is called directly, abort. defined('WPINC') || die(); function do_shortcode_MONSHORTCODE($atts) { $html = '<div id="#info_societaire">'; if (is_admin()) { // ... } elseif (wp_doing_ajax()) { // ... } else { // Parse the shortcode's options. $args = shortcode_atts( array( // 'ipsum' => '', // 'paragraphs' => FIS_DEFAULT_IPSUM_PARAGRAPHS, 'debug' => false, ), $atts ); if ( !is_user_logged_in() ) return ""; //$html.="id: ".get_current_user_id(); $numper = get_user_meta( get_current_user_id(), 'meta_name', true); if ( empty($numper) ) return "meta non trouvée"; $url = 'https://site.com/api/?numper='.$numper; // Create a Curl Handle. $curl = curl_init(); // Standard PHP/Curl options for a simple GET request. curl_setopt_array($curl, [ CURLOPT_URL => $url, CURLOPT_SSL_CIPHER_LIST => 'DEFAULT@SECLEVEL=1', CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", ]); // Execute the request. $json_response = curl_exec($curl); $err = curl_error($curl); // Close the Curl Handle. curl_close($curl); if ($err) { $html .= ''; } elseif (empty($json_response)) { // No data returned from the server. $html .= ''; } elseif (boolval($args['debug'])) { // Render the raw JSON response. $html .= '<pre>'; $html .= sprintf("HTTP_RESPONSE=%d\n", $response_code); $html .= sprintf("%s", $json_response); $html .= '</pre>'; } elseif (empty($data = json_decode($json_response))) { $html .= sprintf( '<p><strong>ERROR:</strong> Failed to process the JSON response.</p><pre>%s</pre>', esc_html($json_response) ); } else { if( !isset($data->result) or $data->result != 'OK' ) $html.="<p>Données indisponible</p>"; else{ $html.=""; } } } $html.="</div>"; return $html; } add_shortcode('MONSHORTCODE', 'do_shortcode_MONSHORTCODE');
Exécuter le shortcode dans un template
<?php echo do_shortcode("[shortcode]"); ?>