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
- is_plugin_active function doesn’t exist
- How to call a plugin function from index.php
- Rewriting every url
- How can I make my custom shortcode work in a Custom HTML Widget?
- How Does WordPress Uninclude/Deactivate A Plugin?
- How to pass multiple values in shortcode?
- Ajax call doesn’t work in frontend but it’s working in backend (when I’m logged in)
- How can I add a custom meta value on file upload?
- Add a Custom Permalink option in the Permalink Admin Screen?
- Allow Facebook to preview posts before published
- publish_post plugin hook doesn’t always pass $post->post_content
- Let user subscribe to specific categories
- May i Use ShortCode in Template?
- Best way to hook a custom url?
- How to cancel WordPress’ action/filter when using OOP with anonymous callback
- What is the best way to mimic a search query within a plugin
- Programatically download a plugin
- Do action only on certain front end pages?
- Call to a member function have_posts() on a non-object on normal loop
- Archive – same title for the first two posts
- Creating plugin using simple_html_dom parser?
- Not able to add option in Sub-Menu under page
- Create a navbar filter that filters by a custom field
- wp.media javascript issue with on select
- Change plugin name on WordPress repo
- Admin AJAX doesn’t work in plugin admin page – Even though code is copied verbatim from WordPress Codex
- Are plugins instantiated on every request to WordPress?
- Why shortcode always displayed after footer not in body
- adding a button to the media uploader
- How to delete all categories programatically?
- How to use custom Javascript code inside a plugin?
- How to render a template file using shortcode inside a plugin
- Executing Javascript in Plugin
- Does WordPress check for updates of a plugin via plugins root folder name?
- Delete data from custom table when deleting a post
- How do i ‘deactivate’ a plugin only on a certain page template?
- Need to add/remove group of options and display them as rows
- Paypal Framework
- Order by post_date does not work in Previous and Next post pagination
- Fatal error “Call to undefined function is_plugin_active” each time the plugin is activated
- How to check if short code is present in template?
- How to get the checkout form data from checkout page when place-order button is clicked
- Getting Fatal error: Uncaught Error: Call to undefined function plugin_dir_path() when linking to another file within my wordpress plugin
- How to make WooCommerce multiple filter with custom meta fields
- WordPress show descriptions under images in gallery
- Call function when save setting/option in custom admin page
- Plugin to Delete All Posts Monthly
- mailjet plugin – extend functionnalities and reuse api
- Can anyone tell me why I can’t edit a plugin when it is installed without having to re-install?
- Creating fields in the database
- WP Elementor Pro causing crtical failure on website
- plugin doesn’t retrieve data from database
- Display Any Field fromAdmin Panel in Frontend via Shortcode?