1

I'm using exceptions in my plugin.

function foo() {
    // Some code.
    throw new Exception( 'Division by 0' );
    // Code continued.
}

try {
    echo foo();
} catch ( Exception $e ) {

}

I'm aware $e->getMessage() will give me the caught exception's message, but in the absence of a notifications API, what is the recommended way to display this message to the user on the front-end?

1 Answer 1

0

For admin area you can use admin_notices:

https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices

For public area (front end) you would probably need to output to some hook and insert the proper CSS. You could use wp_footer() for that, which is present in every theme (or at least should be) and is inside the body tag.

2
  • wp_head is triggered in the <head> section. I think notification messages should be displayed in the body? But I'm now thinking which body template hook is guaranteed to always be there? Commented Jan 22, 2016 at 13:33
  • Ok, my fault. Use wp_footer (it's inside body) and it's used by every theme (or at least should be used). From there you can just insert a message with a fixed animation at top, or a lightbox ;) Commented Jan 22, 2016 at 17:54

Your Answer

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

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