I've built a plugin to display our customer feedback on our WordPress website. We use an external feedback system called 'Customer Thermometer'. They have a simple API to use a URL to request stats and comments etc. This example is showing total stats but I have a bigger comments block function that shows recent comments in a slider.
Sample URL to get the total responses back: https://app.customerthermometer.com/api.php?apiKey=[apiKey-here]&getMethod=getNumResponsesValue
I can get the responses back fine using the code below but the cURL request and code I implement is always at the top of the page regardless of where I add the shortcode.
Call method in my class 'picts_ct_api_connection':
public function call($call) {
$url = $this->get_url();
$apikey = $this->get_api_key();
$method = $this->build_apicall($call);
$request_url = $url.$apikey.$method;
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Shortcode function to display response
function picts_ct_responses( $atts = [], $content=null, $tag='' ) {
$method = array(
'getMethod' => 'getNumResponsesValue'
);
$api_call = new picts_ct_api_connection();
$response = $api_call->call($method);
echo $response;
}
add_shortcode( 'total_responses', 'picts_ct_responses' );
I've added my shortcode here (shown below) in the middle of some dummy text on my page:
But it shows on my page here:
I also get a JSON responses error when saving the page so I feel it's something to do with the cURL response printing to the page early and not being added to my variable and being used in the correct place.
I've searched online and not found anything to help on this. Any help would be much appreciated.
John
echo $response
and instead, usereturn $response
in yourpicts_ct_responses()
function.