{"id":1966,"date":"2023-04-05T09:30:00","date_gmt":"2023-04-05T15:30:00","guid":{"rendered":"http:\/\/racmanuel.dev\/?p=1966"},"modified":"2023-04-03T20:16:20","modified_gmt":"2023-04-04T02:16:20","slug":"uso-de-la-http-api-de-wordpress-en-un-plugin-personalizado-creando-peticiones-http-de-forma-eficiente","status":"publish","type":"post","link":"https:\/\/racmanuel.dev\/en\/uso-de-la-http-api-de-wordpress-en-un-plugin-personalizado-creando-peticiones-http-de-forma-eficiente\/","title":{"rendered":"Using the WordPress HTTP API in a Custom Plugin: Creating HTTP Requests Efficiently"},"content":{"rendered":"<h2 class=\"wp-block-heading\" id=\"introduccion\">Introduction:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The WordPress HTTP API is a powerful tool that allows developers to efficiently and compliantly make HTTP requests in their custom plugins. In this article, we&#039;ll explore how to use the HTTP API in a custom WordPress plugin and discuss the main features that facilitate communication with external APIs.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"2048\" height=\"1532\" src=\"http:\/\/racmanuel.dev\/wp-content\/uploads\/64962aaa842e7c325.29050398-2048x1532-1.jpg\" alt=\"Food and Coding at NorthEnd Coffee shop, Banani, Dhaka\" class=\"wp-image-1993\" title=\"\" srcset=\"https:\/\/racmanuel.dev\/wp-content\/uploads\/64962aaa842e7c325.29050398-2048x1532-1.jpg 2048w, https:\/\/racmanuel.dev\/wp-content\/uploads\/64962aaa842e7c325.29050398-2048x1532-1-600x449.jpg 600w, https:\/\/racmanuel.dev\/wp-content\/uploads\/64962aaa842e7c325.29050398-2048x1532-1-300x224.jpg 300w, https:\/\/racmanuel.dev\/wp-content\/uploads\/64962aaa842e7c325.29050398-2048x1532-1-1024x766.jpg 1024w, https:\/\/racmanuel.dev\/wp-content\/uploads\/64962aaa842e7c325.29050398-2048x1532-1-768x575.jpg 768w, https:\/\/racmanuel.dev\/wp-content\/uploads\/64962aaa842e7c325.29050398-2048x1532-1-1536x1149.jpg 1536w\" sizes=\"(max-width: 2048px) 100vw, 2048px\" \/><figcaption class=\"wp-element-caption\"><a href=\"https:\/\/wordpress.org\/photos\/photo\/64962aaa84\" rel=\"nofollow noopener\" target=\"_blank\">Food and Coding at NorthEnd Coffee shop, Banani, Dhaka<\/a> by <a href=\"https:\/\/kausaralam.com\" rel=\"nofollow noopener\" target=\"_blank\">Kausar Alam<\/a> is licensed under <a href=\"https:\/\/creativecommons.org\/publicdomain\/zero\/1.0\/\" rel=\"nofollow noopener\" target=\"_blank\">CC-CC0 1.0<\/a><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"que-es-la-http-api-de-wordpress\">What is the WordPress HTTP API?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The HTTP API is a set of functions included in the WordPress core that facilitates making HTTP requests to remote servers. It offers support for multiple transport methods, such as cURL and streams, and provides a unified interface for interacting with other web services.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"funciones-principales-de-la-http-api-de-wordpress\">Main functions of the WordPress HTTP API<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The main functions of the HTTP API include <code>wp_remote_get()<\/code>, <code>wp_remote_post()<\/code>, <code>wp_remote_request()<\/code> and other auxiliary functions such as <code>wp_remote_retrieve_body()<\/code>. These functions allow you to make GET, POST, and custom requests, as well as process HTTP responses.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creando-un-plugin-personalizado-que-utiliza-la-http-api\">Creating a custom plugin that uses the HTTP API<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"paso-1-crear-el-archivo-del-plugin\">Step 1: Create the plugin file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new PHP file in the folder <code>wp-content\/plugins<\/code> of your WordPress installation. For example, you could call it <code>my-http-api-plugin.php<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"paso-2-agregar-informacion-basica-del-plugin\">Step 2: Add basic plugin information<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code to the created file to provide basic information about your plugin:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/*\nPlugin Name: My HTTP API Plugin\nDescription: Un plugin personalizado que utiliza la HTTP API de WordPress para realizar peticiones HTTP.\nVersion: 1.0\nAuthor: Tu nombre\n*\/\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"paso-3-utilizar-la-http-api-para-realizar-una-peticion-get\">Step 3: Use the HTTP API to make a GET request<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code to your plugin file to make a GET request to an external API (in this case, the JSONPlaceholder API) and display the response in the WordPress admin area:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">function my_http_api_plugin_request() { $url = &amp;#039;https:\/\/jsonplaceholder.typicode.com\/posts\/1&amp;#039;; $response = wp_remote_get( $url ); if ( is_wp_error( $response ) ) { return &amp;#039;Error: &amp;#039; . $response-&amp;gt;get_error_message(); } return wp_remote_retrieve_body( $response ); } function my_http_api_plugin_admin_page() { $response = my_http_api_plugin_request(); ?&amp;gt;\n    &lt;div class=&quot;wrap&quot;&gt;\n        &lt;h1&gt;My HTTP API Plugin&lt;\/h1&gt;\n        &lt;p&gt;Response from JSONPlaceholder API:&lt;\/p&gt;\n        &lt;pre&gt;&lt;?php echo esc_html( $response ); ?&gt;&lt;\/pre&gt;\n    &lt;\/div&gt;\n    &amp;lt;?php\n}\n\nfunction my_http_api_plugin_menu() {\n    add_menu_page(\n        &amp;#039;My HTTP API Plugin&amp;#039;,\n        &amp;#039;HTTP API Plugin&amp;#039;,\n        &amp;#039;manage_options&amp;#039;,\n        &amp;#039;my-http-api-plugin&amp;#039;,\n        &amp;#039;my_http_api_plugin_admin_page&amp;#039;\n    );\n}\nadd_action( &amp;#039;admin_menu&amp;#039;, &amp;#039;my_http_api_plugin_menu&amp;#039; );\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code creates a new page in the WordPress admin area that displays the response from the JSONPlaceholder API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The WordPress HTTP API is an essential tool for developers who want to interact with external web services and APIs in their custom plugins. Leveraging these features in your plugin will allow you to create more robust and flexible solutions that integrate seamlessly with the WordPress ecosystem.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to the basic functions mentioned in this article, the WordPress HTTP API also offers advanced features such as <code>wp_remote_retrieve_headers()<\/code> y <code>wp_remote_retrieve_response_code()<\/code> To access more detailed information about HTTP responses, you can also customize HTTP requests using additional arguments, such as adding custom headers or configuring timeouts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To further enhance your WordPress plugin development skills and HTTP API usage, I recommend exploring the official WordPress documentation, as well as searching for tutorials and examples from the developer community. As you become more familiar with the HTTP API, you&#039;ll be able to create more powerful and efficient custom plugins that enrich the experience for WordPress users and administrators.<\/p>\n\n\n<style><\/style><style><\/style>\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<h4 class=\"wp-block-heading has-large-font-size\" id=\"necesitas-ayuda-con-un-proyecto-web\">Do you need help with a web project?<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Do you need help with a web project? Don&#039;t hesitate to contact me. I develop complete and customized solutions with WordPress and PHP, using modern tools and processes, HTML, CSS, SCSS, PHP, JavaScript, Bootstrap, and more. Ready? Send me a message and let&#039;s talk about your web project!<\/p>\n\n\n<div class=\"wp-block-groundhogg-forms\">\n\t<div class=\"gh-form-wrapper\"><form method=\"post\" class=\"gh-form gh-form-v2\" target=\"_parent\" enctype=\"multipart\/form-data\" name=\"Formulario de Contacto\" id=\"gh-form-24\" data-id=\"24\" action=\"\"><div class=\"gh-form-fields\"><div class=\"gh-form-column col-1-of-2\"><label for=\"first_name\">Name <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><input type=\"text\" name=\"first_name\" id=\"first_name\" class=\"gh-input gh-first-name\" value=\"\" required><\/div><\/div><div class=\"gh-form-column col-1-of-2\"><label for=\"last_name\">Last name(s) <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><input type=\"text\" name=\"last_name\" id=\"last_name\" class=\"gh-input gh-last-name\" value=\"\" required><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"email\">Email <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><input type=\"email\" name=\"email\" id=\"email\" class=\"gh-input gh-email\" value=\"\" required><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"primary_phone\">Phone or WhatsApp <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><input type=\"tel\" name=\"primary_phone\" id=\"primary_phone\" class=\"gh-input\" value=\"\" required><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"nombre_de_tu_negocio_o_empresa\">Name of your Business or Company<\/label><div class=\"gh-form-input-field\"><input type=\"text\" name=\"nombre_de_tu_negocio_o_empresa\" id=\"nombre_de_tu_negocio_o_empresa\" class=\"gh-input\" value=\"\"\/><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"link_de_tu_pagina_web\">Link to your website<\/label><div class=\"gh-form-input-field\"><input type=\"url\" name=\"link_de_tu_pagina_web\" id=\"link_de_tu_pagina_web\" class=\"gh-input\" value=\"\" placeholder=\"If your business or company has a website, enter the link here.\"\/><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"question_type\">What kind of question do you have? <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><select name=\"question_type\" id=\"question_type\" class=\"gh-input\" required><option value=\"Cotizaci\u00f3n\">Price<\/option><option value=\"General\">General<\/option><option value=\"Soporte\">Medium<\/option><\/select><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><label for=\"message\">Ask <span class=\"required\">*<\/span><\/label><div class=\"gh-form-input-field\"><textarea name=\"message\" id=\"message\" class=\"gh-input\" rows=\"7\" placeholder=\"Your question or doubt...\" type=\"text\" required><\/textarea><\/div><\/div><div class=\"gh-form-column col-1-of-1\"><div class=\"consent gh-gdpr\" id=\"gdpr_consent\"><div ><label class=\"gh-checkbox-label\"><input type=\"checkbox\" name=\"data_processing_consent\" id=\"data-processing-consent\" class=\"\" value=\"yes\" required><span class=\"checkbox-label\">I agree to racmanuel.dev&#8217;s storage and processing of my personal data. <span class=\"required\">*<\/span><\/span><\/label><\/div><div ><label class=\"gh-checkbox-label\"><input type=\"checkbox\" name=\"marketing_consent\" id=\"marketing-consent\" class=\"\" value=\"yes\"\/><span class=\"checkbox-label\">I agree to receive marketing offers and updates from racmanuel.dev.<\/span><\/label><\/div><\/div><\/div><div class=\"gh-form-column col-1-of-3\"><button type=\"submit\" class=\"gh-submit gh-button primary\" value=\"\">Get in touch!<\/button><\/div><\/div><input type=\"hidden\" name=\"trp-form-language\" value=\"en\"\/><\/form><\/div><\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>The WordPress HTTP API is a powerful tool that allows developers to efficiently and compliantly make HTTP requests in their custom plugins. In this article, we&#039;ll explore how to use the HTTP API in a custom WordPress plugin and discuss the main features that facilitate communication with external APIs.<\/p>","protected":false},"author":1,"featured_media":1991,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_gspb_post_css":"","_glsr_average":0,"_glsr_ranking":0,"_glsr_reviews":0,"footnotes":""},"categories":[48],"tags":[42],"niveles":[39],"class_list":["post-1966","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutoriales","tag-wordpress","niveles-avanzado"],"blocksy_meta":[],"acf":[],"_links":{"self":[{"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/posts\/1966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/comments?post=1966"}],"version-history":[{"count":4,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/posts\/1966\/revisions"}],"predecessor-version":[{"id":1994,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/posts\/1966\/revisions\/1994"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/media\/1991"}],"wp:attachment":[{"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/media?parent=1966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/categories?post=1966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/tags?post=1966"},{"taxonomy":"niveles","embeddable":true,"href":"https:\/\/racmanuel.dev\/en\/wp-json\/wp\/v2\/niveles?post=1966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}