WordPress and error handling

0
385

Error handling in WordPress falls partly under common error handling in PHP and partly under WordPress-specific error handling. In this article, we will see how to handle errors in WordPress.

Syntax errors

The most common errors are those concerning PHP syntax. The best way to prevent these types of errors is to get an editor to check the syntax before saving the file to the server. To avoid using the internal editor of WordPress, because the latter not only does not have the most basic functions but does not perform any kind of control on the PHP syntax.

Syntax errors generate a Fatal Error in PHP that completely blocks the execution of the rest of the code. If your server is configured not to show PHP error messages but to record them in a log file, the error returned by the server will be 500.

Errors deriving from undefined classes and functions

The PHP interpreter blocks code execution when trying to use a function that has not been defined. The most typical case in WordPress occurs when we use a function of a plugin in our theme and then disable the plugin. Disabling the plugin the function no longer exists in the WordPress stream, so it is undefined. Preventing this type of error is possible through the PHP function function_exists():

if (function_exists ('my_plugin_func')) {
    my_plugin_func ();
}

Similarly, we can verify that a class has been defined using the PHP function class_exists():

if (class_exists ('MyClass')) {
    $ myClass = new MyClass ();
}

The most common mistake with classes occurs when we try to invoke a method of a class. In this case, the PHP interpreter will signal us that we are trying to use a method on a non-object ( non-object ).

Errors resulting from unexpected input

Suppose we have created a function that generates a loop through the class WP_Querystarting from an associative parameter array passed to the function. What if the function input is not an array?

function make_loop ($ args) {
    $ loop = new WP_Query ($ args);
    // ...
}

There are two solutions to this problem:

  • Use a default value
  • Perform a data check

Here is the first solution:

function make_loop ($ args = array ()) {
    $ loop = new WP_Query ($ args);
    // ...
}

This solution can be further improved:

function make_loop ($ args = array ()) {
    $ defaults = array (
        'posts_per_page' => 3
    );
    $ args = wp_parse_args ($ args, $ defaults);
    <font></font>
    $ loop = new WP_Query ($ args);
    // ...
}

In this way, it will have predefined arguments that will allow it to return an output anyway.

The second solution is to use PHP data type verification features:

function make_loop ($ args) {
  $ loop_args = array ();
  if (is_array ($ args) && count ($ args)> 0) {
    // is an array and is not empty
    $ loop_args = $ args;
  } else {
    $ loop_args = array (
        'posts_per_page' => 3
    );  
  }
  $ loop = new WP_Query ($ loop_args);
    // ...
}

This approach is not only valid for functions, but more generally for all those routines that require the use of an input defined by external code.

Errors arising from the return values of WordPress functions

Consider the following code:

global $ post;
$ id = $ post-> ID;

$ terms = get_the_terms ($ id, 'nometassonomia');

foreach ($ terms as $ term) {
    echo $ term-> name. </font><font style="vertical-align: inherit;">"\ N";
}

The underlying error lies in the fact that we did not consider the return values of the function get_the_terms()if there are no terms associated with the post or the specified taxonomy does not exist; then:

global $ post;
$ id = $ post-> ID;
$ terms = get_the_terms ($ id, 'nometassonomia');
if ($ terms &&! is_wp_error ($ terms)) {
    foreach ($ terms as $ term) {
        echo $ term-> name. </font><font style="vertical-align: inherit;">"\ N";
    }
}

The Returns section (return values) of the WordPress documentation for this function states that:

Array of term objects on success; falseif the post contains no terms from the given taxonomy; falseif the post does not exist; and Class_Reference/WP_Erroran object if an invalid taxonomy is entered.

The function is_wp_error()verifies that the returned value is an instance of the core class WP_Error. This class can also be used to manage our errors:

function my_func ($ param) {
    if (! $ param) {
        return new WP_Error ('error-code', 'Error message');
    }
}

$ return = my_func ();
if (is_wp_error ($ return)) {
    echo $ return-> get_error_message (); </font><font style="vertical-align: inherit;">// 'Error message'
}

The class constructor accepts three parameters:

  • a string representing the error code
  • the error message associated with the code
  • data associated with the error in the form of an array

You can find more information about this class in the official documentation.

LEAVE A REPLY

Please enter your comment!
Please enter your name here