Following would be the revised example code which you may want to try.
function display_api_response() {
$api_url = "https://randletter2020.herokuapp.com";
$response = wp_remote_get($api_url);
if ( 200 === wp_remote_retrieve_response_code($response) ) {
$body = wp_remote_retrieve_body($response);
if ( 'a' === $body ) {
echo 'A wins';
}else {
// Do something else.
}
}
}
add_action( 'init', 'display_api_response' );
Here, you may want to replace init action with something else suitable to your requirements.
Please refer wp hooks for appropriate actions: https://codex.wordpress.org/Plugin_API/Action_Reference
Related Posts:
- How to add a shortcode button to the TinyMCE editor?
- Custom media upload content for inserting custom post shortcode
- Checking if an attribute exists in a shortcode
- enqueue script only if it is not already enqueue
- Enqueue style inside shortcode but its loaded at the bottom of page (before footer scripts)
- How To Ignore a Filter On Applying Filter the Content In a Function
- Shortcode display outside the div
- Pass $this to function nested in another public function of the same class
- shortcode doesn’t work
- How to add inline css/js inside a shortcode
- Only execute jQuery function(on document ready) on the page has shortcode from plugin [duplicate]
- Demystifying and understanding shortcode nomenclature
- Any Short code Availble for Get Post List With Thumbnail Plugin?
- Where to call add_shortcode function in WordPress Plugin Boilerplate?
- Input with spaces in Shortcode attributes overwritten by defaults
- Allow users of my plugin to define their own shortcode rather than use mine?
- Making a Template for a CPT created by a plugin
- creating html reusable blocks via shortcodes
- Single API call exposed via shortcode with params
- Creating shortcodes in plugin
- How to create a custom shortcode based on the layout?
- Inject HTML meta tag inside wordpress tag using add_shortcode
- Create shortcode to echo javascript
- Redirect to another page using contact form 7? [closed]
- Namespaced shortcode?
- How can I run a custom shortcode function on a live site and only run if the viewer is a specific machine?
- Why am I unable to load scripts in head in plugin?
- How to add a dvi tag to a shortcode then change a generated text using jQuery
- Shortcode Attributes to Return different $_POST
- How to use template inside plugin shortcode with variables for big HTML code
- strange shortcode error: does shortcodes requires any dependency?
- How to rewrite URL and get the values?
- Thumbnail image doesn’t show up in Shortcode output
- list of custom post by custom field in frontend
- Shortcode do not return the right data in post
- How to call function in WordPress on button click?
- Hide content for specific users with id
- Why is my shortcode not working?
- WP Plugin CSS not being applied to page
- Modify Plugin PHP Class in Child Theme – Correct Method
- Shortcode in a blog post, footer and related products stop working
- shortcode which is introduced into entry the blog, and appears in side bar
- Why function hooked using object are executing at all time?
- Shortcode registered from a plugin not recognized
- Any way to hook into WP after a page displays?
- Shortcode cannot parse attributes within double quotes. ” is becoming ” breaking my shortcode
- Display file contents within Plugin
- How to get next day date of a specific day
- Update plugin from personal API
- Symlinked plugin directory doesn’t appear in Admin
- How can I include a setting that has a variable number of values in a settings page using register_setting?
- change plugin shortcode function [closed]
- Custom data-id wp_enqueue_script
- How use %like% in sql statement wordpress
- Can I use a form in a dashboard widget?
- Is there a way to alter the order in which the plugins appear in the page?
- MITM risk of not sanitizing?
- Get IDs of Images from Gallery Block in InnerBlocks of a Custom Gutenberg Block
- Change plugin main file name (that is currently in the repo)
- Edit Yoast SEO breadcrumbs output [closed]
- Making sure that a plugin’s scripts and stylesheets are included for dynamicaly loaded content
- Suggestions on Approach to New Plugin I’m Stuck On
- How to redirect users to their profile after they successfully edit their profile
- Can a Plugin Override New User Default Role Type
- Great blogs site resources/tutorial for WordPress Developers? [closed]
- How to make multiple admin pages for one plugin?
- Create custom pages with a plugin
- Is it possible for a plugin to force reading and writing of uploaded images to a database instead of to a filesystem?
- Amazon.com intergration with WordPress?
- Change letters for day name
- How to use add_action for multiple instances of the same class
- Plugin option default value
- How can i listing current category and Featured Category post list?
- save_post_product action not firing
- How to get the custom field value using SQL query
- specific rows style for in custom list table using WP_List_Table
- How to add the search page link to the anchor tag?
- Why does my custom plugin only function correctly once per page?
- How to pass multiple custom fields as shortcode’s parameters
- Custom plugin settings page with tab. Error: “Sorry, you are not allowed to access this page.”
- return bbp_get_topic_author_id as integer
- I am Getting Sorry, no posts matched your criteria on form submit only for certain page [duplicate]
- Dynamic input fields in WordPress Plugin
- Custom role not listed
- Get shortcode attribute for AJAX call
- How to add a box note in wordpress plugin page ? top header side.
- WordPress Widget: Is it possible to make the form dynamic without updating?
- Why some plugins are including wp-includes/plugin.php file?
- Change default url to plugin custom post type
- Allow user to input shortcode into wordpress widget
- How can I pass value to function in add_menu_page?
- Making a Custom Post Type Publish Loop
- How to hide a piece of code from a wordpress web page?
- How to dispaly post informations (such as titles) in an admin plugin menu page?
- Someone Comment 1000 times in a Day in My WordPress Site, What Can i Do
- wp_handle_upload – specified file failed upload test
- how to make wordpress remember my choice
- wordpress admin-ajax bad request 400
- Unable to Edit or Add Photo Gallery Under Portfolio in WordPress 6.2.5
- Remove and strip html tag values