1

I'm working on a WordPress plugin that returns the follower count for a specific Forrest user.

Would like the function to gracefully return 0 if there was an error communicating with the API server or any other problems.

Here is the function:

/**
 * Get Forrst followers.
 *
 * @param string $forrstID The username of the Forrst member
 * @return int. Number of Forrst Followers
 */
function ass_get_forrst($forrstID) {
    $json = wp_remote_get("http://forrst.com/api/v2/users/info?username=".$forrstID);

    if(is_wp_error($json))
        return false;

    $forrstData = json_decode($json['body'], true);

    return intval($forrstData['resp']['followers']);
} 

I have a block in the function to return false if there was an error however it seems that this section must be skipped as sometime I still end up with "Fatal Errors" IE Maximum execution time exceeded.

Is there a better way I could rewrite this function to return "0" if there was an error. Maybe a Try/Catch block?

Do I have the if(is_wp_error($json)) return false; in the wrong section of the function?

1
  • Yes, correct. If there is an error communicating with the server for instance a fatal error will be returned instead of just returning. Commented Nov 22, 2012 at 19:29

2 Answers 2

1

I do not know about wordpress model, but sounds like one of the two functions that you are using is throwing an exception. In this case only a Try/Catch block can smoothly return a cero as you want

what if(is_wp_error($json)) is checking (I guess) is over some "know" set of errors previously detected by wordpress.

you function with a "generic" try/catch block:

function ass_get_forrst($forrstID) {
    try {
        $json = wp_remote_get("http://forrst.com/api/v2/users/info?username=".$forrstID);

        if(is_wp_error($json))
            return false;

        $forrstData = json_decode($json['body'], true);

        return intval($forrstData['resp']['followers']);
    } catch (Exception $e) {
            return false;      // as above

    }
} 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Luis, I will try this method.
1

Register a shutdown function:

function returnzero() {
        $error = error_get_last();
        if($error &&  ['type'] == E_ERROR){
            echo 0;
        }
    }
register_shutdown_function('returnzero');

Note that you might want to turn off error reporting on this page, using something like:

error_reporting(E_ALL & ~ E_ERROR);

3 Comments

Thanks Asad, do I need to call this function from within the above function or will it be called by WP?
@Jason Just add it to the end of the php script that is supposed to return a zero instead of an error.
sorry but, the callback function that is executed with register_shutdown_function CAN'T return to the normal execution flow, so the execution will END even in this case. Is true that it will echo 0; // or whatever but the rest of wordpress run will be lost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.