How to debug new shortcode? And how to get string from shortcode into code?
I am trying to write a simple plugin that fetches some data from an API endpoint. I am planning to read the api key from a shortcode, but didn't get that far yet.
I wrote the following piece of code. The noob question I have is how do I even trigger the code so that I could debug it to see what happens ?
If that's a simple question, the follow up would be how to read the api key from a shortcode?
class DATA_PARSING
{
private static $instance;
/**
* Initializes the plugin and read some data
*
* @access private
*/
private function __construct()
{
add_action('data', [$this, 'fetchData']);
}
/**
* Creates an instance of this class
*
* @access public
* @return DATA_PARSING An instance of this class
*/
public function get_instance()
{
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
private function fetchData($apiKey)
{
$url = 'https://api.website.com/data';
$args = [
'id' = 1234,
'fields' = '*'
];
$method = 'GET';
$headers = array(
'Authorization' = 'Bearer ' . $apiKey,
'Accept' = 'application/vnd.website.v1+json',
'content-type' = 'application/json',
);
$request = array(
'headers' = $headers,
'method' = $method,
);
if ($method == 'GET' !empty($args) is_array($args)) {
$url = add_query_arg($args, $url);
} else {
$request['body'] = json_encode($args);
}
$response = wp_remote_request($url, $request);
try {
$json = json_decode($response['body']);
} catch (Exception $e) {
$json = null;
}
return $json;
}
}
Topic plugin-json-api wp-remote-request plugin-development Wordpress
Category Web