Home    Articles


Wordpress - Displaying errors in Admin Area on form submit / post update


To show errors in wordpress admin, simply there is no "text book" way. I was looking for a nice and easy way but i just recently found one and used it. Of course there are many ways but here i will show you just one.

Let's say you want to check if some fields are filled up before updateing the post in admin area.

This is the code:

* Function that checks for fields
function sc_check_fields($post_id) {
    // check for something more here 
    if(is_admin() && ...etc ) {
        if(!isset($_POST['fields']) || empty($_POST['fields'])) {
                $error = new WP_Error('_error_failed', 'Fields does not exists.');
                if ( !session_id() ) {
                // save error message in the session
                $_SESSION['sc_errors'] = $error->get_error_message();
                set_transient("sc_errors", $error, 45);
                // redirect and prevent the code to update the post anyway
                header('Location: '.site_url().'/wp-admin/post.php?post='.$post_id.'&action=edit');exit;
add_filter( 'pre_post_update', 'sc_check_fields', '10', 1);
* admin notification
function sc_error_message() {
    if ( $error = get_transient("sc_errors")) { ?>
        <div class="error">
            <p><?php echo $error->get_error_message(); ?></p>
        if(isset($_SESSION['sc_errors'])) {
add_action( 'admin_notices', 'sc_error_message' );

That should be all, you should adapt this code.