0

I'm in the process of creating a WordPress plugin that retrieves information from a Minecraft Server. I have the script to do this working fine as just a PHP script, however, when I try to convert that Script to a Plugin, it won't work.

I orignally used cURL functions, but after some googling I learned that that is not allowed within WP, so I changed the cURL functions to wp_remote_get.

The problem is, the shortcodes that I have created will not output anything. I really can't figure out what the problem is, so I decided to post here after a couple days of googling.

Here are the plugin contents: https://gist.github.com/th3no0b/1fa9aba11789160d9e48

(the file directories are right as far as I can see, mcq-core.php, mcq-functions.php and mcq-shortcodes.php files are in the core folder)

0

wp_remote_get doesn't return a string, it returns an array, and if we refer to the documentation:

$response = wp_remote_get( 'http://www.example.com/index.html' );
if( is_array($response) ) {
  $header = $response['headers']; // array of http header lines
  $body = $response['body']; // use the content
}

Here $response would not be a json string, but an array containing the headers and a body, the body containing a json string.

So:

function mcq_getMcApi($type){
    // This function mcq_gets the information from "mcapi.ca" if required
    $json = wp_remote_get("https://mcapi.ca/query/$ip/$type");
    return json_decode($json,true);
}

becomes:

function mcq_getMcApi($type){
    // This function mcq_gets the information from "mcapi.ca" if required
    $response = wp_remote_get("https://mcapi.ca/query/$ip/$type");
    return json_decode($response['body'],true);
}

Note that wp_remote_get will return a WP_Error object that you should check for it any of this fails, and you should check the response, as it may not be a json string.

  • Thanks. I've changed the mcq_getMcApi() function, however there is still no output. – Th3No0b Sep 28 '15 at 17:03
  • I checked if what was being returned was just text, which it wasn't, so I've adjusted that. My function is now like so gist.github.com/th3no0b/3b411cf053b5c8211120 however it still doesn't output anything... :/ – Th3No0b Sep 28 '15 at 17:11
  • Then further debugging is going to be needed, your code contains a large number of assumptions, but never checks these assumptions are true ( e.g. assuming your remote request worked or that it returns a string not an array, it still doesn't check for a WP_Error object ) – Tom J Nowell Sep 28 '15 at 20:53
  • Of note, you use $online in every function, but inside each function that variable is out of scope and thus undefined – Tom J Nowell Sep 28 '15 at 20:55
  • The same is true of all the other variables, they all resolve to '' so your shortcodes output nothing. You should read up on global variables and variable scope, or make use of objects and member variables to store state internally – Tom J Nowell Sep 28 '15 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.