Results 1 to 9 of 9

Thread: add_meta_box - how to display errors

  1. #1
    Rich Pedley's Avatar
    Rich Pedley is offline Hello World
    Join Date
    May 2009
    Location
    Liverpool, UK
    Posts
    92

    Default add_meta_box - how to display errors

    I've been using the add_meta_box functionality for a while in my plugin, and use a save_postdata function to update the post_meta.

    However I now want to add in an error message if certain conditions are not met, I can code for these without any problems. But I can't figure out how to then display an error message. I've tried various things so far, but no joy. Any clues to get me on the right track?

  2. #2
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    The admin-header gets loaded on most every admin page. One of the final things it does is to run the admin_notices action hook. This provides an easy way to add any message you want to the top of the page.

    For consistency, you should use the error class in a div to display your message. Example:

    PHP Code:
    add_action('admin_notices''my_error_message');
    function 
    my_error_message() {
      echo 
    '<div class="error"><p>Error message!</p></div>';

    This causes the nice red error box to appear. You can include links and/or anything else in the error message box.

    But basically, you just do the add_action when you detect your error condition.

    Note: If you just want to display the yellow notice box, not an error, use class="updated" instead.

  3. #3
    Rich Pedley's Avatar
    Rich Pedley is offline Hello World
    Join Date
    May 2009
    Location
    Liverpool, UK
    Posts
    92

    Default

    I must be missing something then.

    I use this:
    PHP Code:
    add_action('save_post''eshop_save_postdata'); 
    Do all my updating of post meta in that function, and right at the end(but still within that function) I have this:
    PHP Code:
    if($stkav!='0' && ($eshop_product['sku']=='' || $eshop_product['description']=='' || $eshop_product['product']['1']['option']=='' || $eshop_product['product']['1']['price']=='')){
       
    update_post_meta$id'_eshop_stock''0');
       
    add_action('admin_notices''eshop_error_message');

    The function, in case it's needed:
    PHP Code:
    function eshop_error_message(){ ?>
       <div class="error fade"><?php _e('<strong>Error</strong> some details were not filled in.','eshop'); ?></div>
      <?php
    }
    Am I using it in the wrong place? As usual with my queries I assume it is something blindingly obvious...

  4. #4
    Rich Pedley's Avatar
    Rich Pedley is offline Hello World
    Join Date
    May 2009
    Location
    Liverpool, UK
    Posts
    92

    Default

    Well I have a solution, albeit not very elegant.

    using this hook to grab and print the error
    PHP Code:
    add_action('admin_head-post.php''add_eshop_notice'); // called after the redirect 
    user transient option to store the error if and when required
    PHP Code:
    if($stkav!='0' && ($eshop_product['sku']=='' || $eshop_product['description']=='' || $eshop_product['product']['1']['option']=='' || $eshop_product['product']['1']['price']=='')){
      
    update_post_meta$id'_eshop_stock''0');
      
    set_transient('eshoperr''error text will go here');

    which when used will add the admin notice
    PHP Code:
    function add_eshop_notice(){
        
    $err=get_transient('eshoperr');
        if(
    $err!=''){
            
    add_action('admin_notices','eshop_error_message');
        }

    using this function to then print it.
    PHP Code:
      function eshop_error_message(){ ?>
            <div class="error fade"><?php echo get_transient('eshoperr'); ?></div>
        <?php
            delete_transient
    ('eshoperr');

      }
    Probably the long way around of doing things, but it does work

  5. #5
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Quote Originally Posted by Rich Pedley View Post
    I must be missing something then.
    ...
    Am I using it in the wrong place? As usual with my queries I assume it is something blindingly obvious...
    Yes. Every time something is posted, the page redirects. You have to detect your error and cause the message display after the redirect, not before it.

    The transient method you're using is one way to do it. Not a good way though. The problem is that transients are there for everybody. If you have more than one user doing things at the same time, the error message can go to the wrong person. It's a race condition.

    WordPress actually does this by passing a message parameter in the URL. The message number indicates which message to display.

    You can do the same by hooking the redirect_post_location filter and then using add_query_arg to add your own parameter to the request. Like so:

    PHP Code:
    add_filter('redirect_post_location','my_message');
    function 
    my_message($loc) {
     return 
    add_query_arg'my_message'123$loc );

    This adds my_message=123 to the query. Then, after the redirect, you can detect the my_message setting in the $_GET and display the proper message accordingly.

  6. #6
    Rich Pedley's Avatar
    Rich Pedley is offline Hello World
    Join Date
    May 2009
    Location
    Liverpool, UK
    Posts
    92

    Default

    should have realised that about transients - and yes it could be an issue for my plugin.

    and thanks, for the that bit of code, that add filter was the bit I couldn't work out.

    This would be so much easier with a native WP add_error function.

  7. #7
    Rich Pedley's Avatar
    Rich Pedley is offline Hello World
    Join Date
    May 2009
    Location
    Liverpool, UK
    Posts
    92

    Default

    got it to work, once again thanks for the help Otto.
    Last edited by Rich Pedley; 03-11-2010 at 06:33 AM.

  8. #8
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    862

    Default

    Quote Originally Posted by Rich Pedley View Post
    This would be so much easier with a native WP add_error function.
    There is such a system (WP_Error) but it has not been implemented everywhere in the admin, and the places it is implemented are somewhat inconsistent.

    Hopefully it gets some improvement in 3.1.

  9. #9
    Rich Pedley's Avatar
    Rich Pedley is offline Hello World
    Join Date
    May 2009
    Location
    Liverpool, UK
    Posts
    92

    Default

    Yeah I did try and use that, but I found it difficult to get my head round - as always.

    My plugin was one of the ones that you hate - multiple options added, and way to many postmeta as well... I've got the options down to 1, and the postmeta to 2. Trying to get my plugin closer to WP standards than it has ever been before!

    So I'm trying to do things the right way, I'll probably miss loads, but at least it's a start.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •