0

I'm trying to restrict how many items you can associate with a post so in my save function I have:

add_action( 'save_post', array( $this, 'save_custom_items_data' ), 10, 2 );

public function save_custom_items_data( $post_id, $post ) {
    if(count($related_items) > 5) {
        // display error message here but the page redirects anyway...
    } else {
        update_post_meta( $post_id, '_custom-meta-items', $related_items );
    }
}

how do I display an error here instead of saving the custom post meta?

2
  • 2
    There's not enough code here to be useful.
    – vancoder
    Apr 8, 2013 at 21:14
  • added some more code
    – keeg
    Apr 9, 2013 at 19:49

1 Answer 1

2

Short of doing some jQuery validation, I think the only option is wp_die().

add_action( 'save_post', array( $this, 'save_custom_items_data' ), 10, 2 );

public function save_custom_items_data( $post_id, $post ) 
{
    if( count( $related_items ) > 5 ) 
    {
        wp_die(
            'Error, 5 items max.', 
            'Error',  
            array( 
                'response' => 500, 
                'back_link' => true 
            )
        );    
    } 
    else 
        update_post_meta( $post_id, '_custom-meta-items', $related_items );
}

Related Q&A's:

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.