function display_api_response() {
$body = get_transient( 'my_remote_response_value' );
if ( false === $body ) {
$api_url = "https://randletter2020.herokuapp.com";
$response = wp_remote_get($api_url);
if (200 !== wp_remote_retrieve_response_code($response)) {
return;
}
$body = wp_remote_retrieve_body($response);
set_transient( 'my_remote_response_value', $body, 5*MINUTE_IN_SECONDS );
}
if ('a' === $body) {
echo 'A wins';
} else {
// Do something else.
}
}
add_action('init', 'display_api_response');
At first, the transient doesn’t exist, so we send a request and save the $body
as a transient value. Next time, if the transient exists, we skip sending request.
Check the Transient Handbook for more information.
Related Posts:
- Can I list all installed plugins/versions and wp version from API
- How do I retrieve a list of popular plugins using the WordPress.org Plugin API?
- WordPress wp-json API – Custom Post Type returns 403
- How to retrieve custom meta term of category taxonomy from WP Rest API?
- How to make Http Request to a php file present in plugin directory of wordpress
- I am getting error message on accessing menu-endpoint
- How to make a implement queue for scheduling tasks in WordPress?
- WP_Http response throws “Cannot use object of type WP_Error as array”
- How can I list all installed plugins/themes/versions from CLI/API?
- http_response_timeout filter not working
- How to create WordPress custom end point with multiple parameters?
- Best way to retrieve data in custom api extended from wp-api
- oAuth2 Authentication in WordPress using WP OAuth Server and WP API plugins
- WP-API Custom Post Type json_no_route
- Need help creating asynchronous data scraper in WordPress
- Escape commas in REST API
- Consume legacy rest api dependent upon WP API plugin
- How to get custom or filtered endpoints in WordPress API?
- WP REST API Access-Control-Allow-Origin response is cached
- How to use wp_http with https-pages?
- How to implement WordPress plugin update that modifies the database?
- Create tabs inside Plugins Admin Page [closed]
- Looking for a Table of Contents (TOC) plugin for WordPress
- Writing test cases for a WordPress Plugin that has translations
- Can’t install new plugins because of the error “Could not create directory”
- Customize plugin update “new version is available” text
- Allowing Two Developers Access to a Plugin on the WordPress.org Plugin Repository?
- Loading shortcode with ajax
- Creating Custom Meta Boxes on Plugin Option Page
- Add .html extension to custom post type taxonomies
- Translate wordpress plugin [closed]
- Styles and Scripts, Selectively enqueue across entire site
- Nested shortcodes
- Exclude custom post_type in sitemap generation?
- Removing WordPress Plugin Menu Item for a specific user
- Is there a neater way to do this?
- How can I use RDFa with WordPress?
- How to add an attribute to the body tag with a plugin?
- Authorization header malformed. [jwt_auth_bad_auth_header]
- Changing image size to full size in Gutenberg’s block gallery
- Trying to count the total number of paragraphs inside a blog article
- Regex to Remove File Extension
- Ultimate Member: Redirecting after registration
- how to create site exit messages with destination url displayed
- Magazine subscription sales through WP?
- File Storage and Repository for individual user
- Changing “elapsed time” to “time remaining” on MP3-jPlayer audio player with CSS or PHP changes
- Ctrl+V images from clipboard into wordpress editor
- Collect user information when click on download link [closed]
- Can’t Install Standard Plugins on a Local MAMP installation
- How to set intervals in cron jobs?
- Why Do I get “wp_remote-get: could not handle for fopen()”?
- Disable a plugin from ever upgrading?
- Custom post types – remove default post supports through empty array?
- How to get a list of bundled products using wp_query in woocommerce
- How to be escape Variables and options when echo?
- Release the plugin in the WordPress repository where redux is used
- Where does MonsterInsights put Google Analytics code?
- Trying to add_action in a loop
- Ajax object not defined – JS file is in plugin folder
- Settings API Message Error is global
- Is there a good way to load tiny_mce_popup.js through editor.windowManager.open?
- Why does “updated_post_meta” did not fire when uploading media other than image?
- Use a custom block in another block
- Hide fields in woocommerce settings
- Help with autofilling field
- I/O Usage goes max. just after every new post published and WP site down
- See which user role / capability is needed to use a plugin
- Personality quiz in wordpress using a plugin
- Adding custom post category to categories widget
- How to ignore fields if empty?
- Custom REST API POST Endpoint Not Working, 404 Error
- Error messages in Multilingual part of the site
- WordPress how do I change images used in plugins?
- Add WooCommerce vendor slug to auto-generated SKU
- Admin login substantially slower on production server
- Can we rename a plugin directory for a already launched plugin?
- Admin Custom Login loses settings on Code and Data migration
- custom REST endpoint not passing body of POST request to callback
- function of parent not working in childtheme
- WordPress core update fails – no issues with plugin updates
- Am I correctly adding styles to plugin?
- How to hook into search results template or query?
- How can I insert a record into a custom table from my custom form in my custom admin page?
- how to create table during plugin installation in side a class
- Can’t redirect to previous page after using GET
- Why function hooked using object are executing at all time?
- Create a blank admin page from plugin
- Private WordPress blog, requires user registration approval, and allowing users to login with OpenID
- Which part of the plugin to change so the updater won’t register the next version?
- Hide/Show panel not showing – ACF
- How to make only selected posts appear on a selected wordpress page
- Any way to hook into WP after a page displays?
- selecting options from another form
- How to remove . (several lines of customized programming code appear here) from wordpress pages
- WordPress Smart Iframe Browser [closed]
- Publish post facebook page & twitter automatically [closed]
- Problems with file_exists() with metabox plugin in WordPress
- How to rewrite URL using .htaccess in WordPress Plugin
- FacetWP custom display based on post type [closed]