WP_Automatic_Updater::has_fatal_error()wp-admin/includes/class-wp-automatic-updater.php | Performs a loopback request to check for potential fatal errors.
|
WP_Site_Health::check_for_page_caching()wp-admin/includes/class-wp-site-health.php | Checks if site has page cache enabled or not.
|
WP_REST_Pattern_Directory_Controller::get_items()wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php | Search and retrieve block patterns metadata
|
get_block_editor_theme_styles()wp-includes/block-editor.php | Creates an array of theme styles to load into the block editor.
|
WP_Site_Health::wp_cron_scheduled_check()wp-admin/includes/class-wp-site-health.php | Runs the scheduled event to check and update the latest site health status for the website.
|
WP_Debug_Data::debug_data()wp-admin/includes/class-wp-debug-data.php | Static function for generating site debug data when required.
|
WP_Site_Health::get_test_rest_availability()wp-admin/includes/class-wp-site-health.php | Tests if the REST API is accessible.
|
WP_Site_Health::get_test_dotorg_communication()wp-admin/includes/class-wp-site-health.php | Tests if the site can communicate with WordPress.org.
|
wp_check_php_version()wp-admin/includes/misc.php | Checks if the user needs to update PHP.
|
wp_edit_theme_plugin_file()wp-admin/includes/file.php | Attempts to edit a file for a theme or plugin.
|
WP_Community_Events::get_events()wp-admin/includes/class-wp-community-events.php | Gets data about events near a particular location.
|
wp_install_maybe_enable_pretty_permalinks()wp-admin/includes/upgrade.php | Maybe enable pretty permalinks on installation.
|
network_step2()wp-admin/includes/network.php | Prints step 2 for Network installation process.
|
themes_api()wp-admin/includes/theme.php | Retrieves theme installer pages from the WordPress.org Themes API.
|
populate_network()wp-admin/includes/schema.php | Populate network settings.
|
get_core_checksums()wp-admin/includes/update.php | Gets and caches the checksums for the given version of WordPress.
|
plugins_api()wp-admin/includes/plugin-install.php | Retrieves plugin installer pages from the WordPress.org Plugins API.
|
wp_get_popular_importers()wp-admin/includes/import.php | Returns a list from WordPress.org of popular importer plugins.
|
wp_credits()wp-admin/includes/credits.php | Retrieves the contributor credits.
|
url_is_accessable_via_ssl()wp-includes/deprecated.php | Determines if the URL can be accessed over SSL.
|
Valid arguments for the second parameter can be found in class-http.php in the header. There is not easy way to reference the list on the current version of this guide so I’m pasting the PHPDoc header here. Hopefully the docs site will expand the WP_Http page or find a way to reference the valid parameters through indirection while the robots build these pages.
* @param string|array $args {
* Optional. Array or string of HTTP request arguments.
*
* @type string $method Request method. Accepts 'GET', 'POST', 'HEAD', or 'PUT'.
* Some transports technically allow others, but should not be
* assumed. Default 'GET'.
* @type int $timeout How long the connection should stay open in seconds. Default 5.
* @type int $redirection Number of allowed redirects. Not supported by all transports
* Default 5.
* @type string $httpversion Version of the HTTP protocol to use. Accepts '1.0' and '1.1'.
* Default '1.0'.
* @type string $user-agent User-agent value sent.
* Default WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ).
* @type bool $reject_unsafe_urls Whether to pass URLs through wp_http_validate_url().
* Default false.
* @type bool $blocking Whether the calling code requires the result of the request.
* If set to false, the request will be sent to the remote server,
* and processing returned to the calling code immediately, the caller
* will know if the request succeeded or failed, but will not receive
* any response from the remote server. Default true.
* @type string|array $headers Array or string of headers to send with the request.
* Default empty array.
* @type array $cookies List of cookies to send with the request. Default empty array.
* @type string|array $body Body to send with the request. Default null.
* @type bool $compress Whether to compress the $body when sending the request.
* Default false.
* @type bool $decompress Whether to decompress a compressed response. If set to false and
* compressed content is returned in the response anyway, it will
* need to be separately decompressed. Default true.
* @type bool $sslverify Whether to verify SSL for the request. Default true.
* @type string sslcertificates Absolute path to an SSL certificate .crt file.
* Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'.
* @type bool $stream Whether to stream to a file. If set to true and no filename was
* given, it will be droped it in the WP temp dir and its name will
* be set using the basename of the URL. Default false.
* @type string $filename Filename of the file to write to when streaming. $stream must be
* set to true. Default null.
* @type int $limit_response_size Size in bytes to limit the response to. Default null.
Get a remote URL
The top comment by Store Locator Plus adds good information but it is hard to read.
To find the arguments you can see WP_Http::request documentation or see below for a formatted PHPDoc version:
Get a remote URL with special arguments
When adding headers, each part of the header has to be a separate element like this:
not like cURL where you can use ‘Content-Type: application/json’
If you load data from an API and have set a domain restriction on the API key (such as only allow on “www.example.com/*”), you need to set the “referer” in “headers” in $args, as wp_remote_get() does not set the referer automatically:
Or, as a string, to work on your localhost as well:
It would be cool to have a link to this somewhere on this page: https://developer.wordpress.org/reference/functions/wp_safe_remote_get/
replace
wp_get_http( $url, $filename )
to get a remote file and write to the file system:Thank you @charlestonsw for pasting in the comments with the meaning of all the args, which lead me to this simple replacement.
It’s better to execute JSON response like that to keep it managed.
An example for reading a JSON response from an API end point with error checking.
Here we are checking for the response code of 200 utilizing the wp_remote_retrieve_response_code() function and also verifying there are no JSON errors during the json_decode() function call utilising the json_last_error() function.
You may come across following error:
http_build_query() expects parameter 1 to be array, string given
That happens when you try using wp_remote_get() with body parameter. This is not a recommended standard and WordPress will not let you do it.
Use wp_remote_post() instead.
Example:
You can use it to fetch external Authenticated APIs
$result = json_decode($responseBody);
if success as it is will return an object and not a array, my friend, so the if will fail even if there are no errors. Consider changing line 4 to:$result = json_decode($responseBody, true);
json_decode()
is native to PHP, and so will never return an instance ofWP_Error
object; that check is unnecessary, or, should be adjusted to check$response
.