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:
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:
* Description: Desc.
* Version: 1.0
* Author: Ava
* Author URI:
**/
ob_start();
function initapi(){
$api_url = ";;
$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' );
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
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
* Description: Desc.
* Version: 1.0
* Author: Ava
* Author URI: https://randletter2020.herokuapp
**/
ob_start();
function initapi(){
$api_url = "https://randletter2020.herokuapp";
$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' );
Share
Improve this question
edited Oct 15, 2020 at 6:30
stealthyninja
1,1301 gold badge15 silver badges21 bronze badges
asked Oct 10, 2020 at 13:49
Ava JuanAva Juan
152 bronze badges
4
- May I know what is the intent of this plugin? Why you are using rest_api_init hook? – Ankit Commented 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 Commented 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 Commented Oct 10, 2020 at 14:55
- Oh, I understood. What others I have change in my code to make it work? – Ava Juan Commented Oct 10, 2020 at 14:58
1 Answer
Reset to default 1Following would be the revised example code which you may want to try.
function display_api_response() {
$api_url = "https://randletter2020.herokuapp";
$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/Plugin_API/Action_Reference