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


you can call the function by putting this code in a template.

echo fetchData('YOUR_API_HERE');

you should change your first line from:

if you're looking to turn it into a shortcode add this line:

add_shortcode( 'fetch-short', 'fetchData'); 

either right above or below your function.

then change your function start to this:

function fetchData( $atts ) {
    $api = $atts['api'];  //this is your new api variable

your shortcode would be

[fetch-short api="YOUR_API"]

be careful to add checks for incase you someone doesn't add an api though. as in set a default or error check if $api is empty.


To run this code to test it you can use the wp-cli tool with the eval-file command. wp-cli eval-file loads up the whole Wordpress infrastructure then runs your PHP file so you can use Wordpress calls like wp_remote_request without having to put this code in a page or pulgin. E.g. write a file called test.php that instantiates the class and calls the methods, then run that with:

wp-cli eval-file test.php

I see that @rudtek has answered how to get the data from the shortcode into your code.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.