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.
<?php
/**
* 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
**/
ob_start();
function initapi(){
$api_url = "https://randletter2020.herokuapp.com";
$wp_response = wp_remote_get($api_url);
if($wp_response){
$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' );