• I was wondering what the reasoning was as to why WP_Error::get_error_message() gets the first error message, as opposed to the most recent error message?

    To me it would seem more sensible if it retrieved the most recent one.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    I’ve no special insight into the logic behind why core developers do things, but I think it is mainly a convenience method. There are many situations where there will only be one message, if any. Thus this method saves you from needing to fuss with getting the zeroth element from the error object. Yeah, it’s not a big deal to get such an element, but novice coders are easily confused by complex data structures. If all you need is the one message, your code is not cluttered by additional code to extract the message. I for one appreciate uncluttered code.

    If you have greater needs where there may be multiple errors, you are free to use WP_Error::get_error_messages() to get either all messages or those of a particular code.

    What I mean, is why did they choose to get the first message though? They could have just as easily decided to return the last message.

    Was it just something that was done without much thought, and then has been kept that way for compatibility reasons? Or is there some good reason that you’d more likely want the first message rather than the last one?

    PS I’m the OP, I must have signed in with one of my other a/cs by accident.

    • This reply was modified 7 years, 7 months ago by djeyewater. Reason: clearing up that I'm the OP
    Moderator bcworkz

    (@bcworkz)

    Thanks for clarifying who you are, that would have been confusing. Different account and even different IP. Please try to be consistent with which account you use. While there are perfectly legitimate reasons for multiple accounts, they can also cast a great deal of suspicion upon yourself. Some misguided plugin and theme authors create multiple accounts so they can tell themselves how great their product is. Such behavior is not tolerated at all and it will not end well for them.

    As I’ve no insight to why core devs do things, I can’t accurately answer. Going off my thought that it’s a method of convenience for situations where only one error can possibly occur, the first message happens to be the last message as well. So in that context they are showing the last message ??

    The bottom line is if you need the last message of several, you are using the wrong method. You want WP_Error::get_error_messages() — the one with an extra S for the plural form of message.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Why does WP_Error::get_error_message() get first message?’ is closed to new replies.