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?
- i need to resive data external api and show user
- Set Multiple Meta Values as an Array Using dispatch( ‘core/editor’ ).editPost() Call in Gutenberg/JS
- wp-admin send 404 error
- How to create plugin/ page that reads from database
- Stop wordpress from requesting external jquery from googleapi
- Can the benefits of performance optimization plugins outweigh the tax of installing them on performance?
- get_users(); Is an Undefined Function When Used in Cron Job
- Alternate email sending service – eg: AWS SES [closed]
- 5 PHP Fatal error: Uncaught ArgumentCountError (Cannot Find)
- Disabling a plugin lead to styles being shown as plain text
- the_content() printing DOCTYPE, and tags in the middle of page
- How to deal with Slow HTTP POST (slowloris) vulnerability
- How to create author profile showcase in wordpress
- What is the purpose of WP_CONTENT_URL?
- Blog previews on custom page with more buttons
- Order posts by meta key ( Using ACF )
- Control page content visibility based on URL parameter
- Where does MonsterInsights put Google Analytics code?
- WooCommerce Shopping Cart Not Initially Showing Products [closed]
- Elementor Custom Control: How to get input value?
- Elementor page builder plugin not loading up all the way? [closed]
- How to add fields in the WordPress editor?
- How can I fetch data from another website to my wordpress website with mysql database
- Sucuri SiteCheck failing on domain with iThemes Security Pro plugin
- How to translate WP Blog (search, category etc.)?
- WordPress SVN UTF-8 issue
- How can I concatenate variables to search results title? [closed]
- Migrating WordPress – New domain entry, links broken
- Search Results Page filter by page title
- Activate and deactivate plugin automatically
- WooCommerce: after install hook
- Test my WordPress Plugin standard or not
- Theme My Login Shortcode Doesn’t Return Anything
- How retreive saved data from gravity forms in my template?
- is_rtl returns false while it should return true
- issue in wp_localize_script
- Import MS-Access data file to make the searchable page
- License validate function
- Sanitize $_GET variable when comparing
- Include Minit plugin in theme [closed]
- Plugin for an interactive image? [closed]
- Where can I save plugin data?
- echo get_option in header template
- How to display all functions (hooks, filters, or custom php) used on a page inline
- wp_enqueue_style on template_redirect level?
- Plugin not working & permalink erorr after upgrade to php 7x on Nginx
- Clear out old images, any advice?
- problem with sql query
- Access post title from custom meta box on title change
- How to add a new column with text fields to WooCommerce Cart List?
- How to use filter to disable adding a product to wishlist?
- Running multiple security plugins
- Save user total active time after login in wordpress [closed]
- add_query_arg not work in admin
- Add a “View all” button on WooCommerce product archives pages
- Opening a JQuery modal window on click of a button with a JQuery plugin
- I am trying to make a field show and save from quickedit screen
- Merge tags in multistep Gravity Form?
- Add to array, redirect and display
- How to track analytics with a WordPress site under the same domain as my main site?
- Can I clone plugin folder to another directory so that I don’t have to reinstall them?
- correctness of URL
- Woocommerce: limit user to see only the products he created
- Woocommerce singe product custom gallery output works just on the first slide
- how do I secure my WP website from hackers? [closed]
- How do I set up XAMPP to allow me to download plugins and themes?
- How can I load the css and jquery explicitly for the login screen
- Overriding an Array in a Plugin’s Class/Function from functions.php
- wp_redirect doen’t work
- Canvas | Bouncing balls within a container (with gravity and collisions and background-images on the balls)
- Plugin translation not working on WordPress.org
- PHP if statement with German umlauts [closed]
- Send email with list of active plugins upon activation/deactivation
- OceanWP settings are missing from Add New Post [closed]
- Built a second plugin but it overwrote the first one
- WordPress gtag.js with User ID tracking
- Creating a functionality plugin to edit seriously simple podcasting
- Override plugin class which has namespace
- in source code but I can’t find it in files or plugins
- Historical customisations won’t go away [closed]