I'm working on a plugin for displaying content based on the Letter Show in API. Where I want to display the content based on A or B returned by the API.

I have created an API here: https://randletter2020.herokuapp.com

The API only displays A or B at a time.

This is the plugin code I have created. I don't know what I'm doing wrong. "A Wins" is not displaying in [letter] I don't know if the API is pinging or not.

I have a bit of coding knowledge. Not an expert. Help me please to fix the issue in my code. Thanks in advance.

* Plugin Name: Based on Random Letter
* Plugin URI: https://randletter2020.herokuapp.com
* Description: Desc.
* Version: 1.0
* Author: Ava
* Author URI: https://randletter2020.herokuapp.com
function initapi(){
$api_url = "https://randletter2020.herokuapp.com";
$wp_response = wp_remote_get($api_url);
$contents = wp_remote_retrieve_body($wp_response);

if ($contents) {
$json = json_decode($contents, 1);
if ($json == "a") {

function set_letter(){
  $letter = "A Wins";
  return $letter;
add_shortcode('letter', 'set_letter');
else {   
        return false;
        //Do nothing
add_action( 'rest_api_init', 'initapi' ); 
  • May I know what is the intent of this plugin? Why you are using rest_api_init hook?
    – Ankit
    Oct 10, 2020 at 14:32
  • Then main intent of the plugin is to Change the content according to the API Response. I thought rest_api_init hook will help because I'm requesting from API.
    – Ava Juan
    Oct 10, 2020 at 14:51
  • rest_api_init hook is used to perform api operation on your WordPress site. Example: Registering a new endpoint.
    – Ankit
    Oct 10, 2020 at 14:55
  • Oh, I understood. What others I have change in my code to make it work?
    – Ava Juan
    Oct 10, 2020 at 14:58

1 Answer 1


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

  • Thank you Ankit. This works really awesome. How can I cache that value in my site (client side) for 5 minutes? Is it possible?
    – Ava Juan
    Oct 11, 2020 at 2:42
  • If client side caching for 5 minutes is possible the API Calls will be in the interval of 5 mins right? Because I want to display same result for 5 Minute. Pinging API changes the API Output value
    – Ava Juan
    Oct 11, 2020 at 2:58
  • In this case, your website is client for the API, so you can use set_transient from wordpress: developer.wordpress.org/reference/functions/set_transient
    – Ankit
    Oct 14, 2020 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.