====== Shortcode ====== --- // source: https://wp-tutorials.tech/add-functionality/access-a-rest-api-from-wordpress/ // '; 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 .= '
';
            $html .= sprintf("HTTP_RESPONSE=%d\n", $response_code);
            $html .= sprintf("%s", $json_response);
            $html .= '
'; } elseif (empty($data = json_decode($json_response))) { $html .= sprintf( '

ERROR: Failed to process the JSON response.

%s
', esc_html($json_response) ); } else { if( !isset($data->result) or $data->result != 'OK' ) $html.="

Données indisponible

"; else{ $html.=""; } } } $html.=""; return $html; } add_shortcode('MONSHORTCODE', 'do_shortcode_MONSHORTCODE');
Exécuter le shortcode dans un template