- How do I prevent that like a misspelling error crash wordpress?
- Why the message it’s not showed ?
- How do I must code this specific situation to see my custom error message?
- Spell your code correctly, to be blunt.
- Because spelling the class incorrectly caused a fatal error.
- Is
CanvasGraph
written to specifically aWP_Error
object under any circumstances?is_wp_error()
is for manually passing error messages around. It’s not for handling generic PHP errors.
Your code should be spelt correctly, it’s that simple. You shouldn’t be writing custom error messages just because you’ve made typos. That implies that you intend to ship code to users you haven’t even bothered to check for typos. Those need to be fixed before you ever ship it to users.
In development the normal PHP errors that will display or be logged should be sufficient for catching these issues. Writing unit tests would be a way to automatically run through all your code if manual testing isn’t feasible, but that’s too large a topic for a single answer here. This article seems to provide a good introduction though.
The purpose of WP_Error
, and is_wp_error()
is for WordPress APIs, or those in your own theme/plugin, to be able to manually return error objects to developers that are using them so that they can handle them downstream. For example, if you tried to use get_terms()
with an invalid taxonomy, it will return a WP_Error
object, because that’s a circumstance that the WordPress developers could anticipate. But if you simply misspell get_terms()
, that’s your mistake and you’ll need to use PHP’s fatal error message to catch the problem, either by checking the error log or running tests.