function write_here_show_error_messages()
{
    if ($codes = write_here_errors()->get_error_codes()) {
        echo '<div class="form-error">';
        // Loop error codes and display errors
        foreach ($codes as $code) {
            $message = write_here_errors()->get_error_message($code);
            echo '<span class="error"><strong>' . __('Error') . '</strong>: ' . $message . '</span><br/>';
        }
        echo '</div>';
    }
}
function write_here_edit_post()
{
    if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == "write_here_edit_post" && wp_verify_nonce($_POST['edit-post-nonce'], 'edit-post')) {
        // Set default date on the post
        $postdate = date('Y-m-d H:i:s');
        // Get values from front end form
        $pid = $_POST['pid'];
        $wh_month = $_POST['mm'];
        $wh_day = $_POST['jj'];
        $wh_year = $_POST['aa'];
        $wh_hour = $_POST['hh'];
        $wh_min = $_POST['mn'];
        $wh_sec = $_POST['ss'];
        $title = wp_strip_all_tags($_POST['title']);
        $content = $_POST['wh_content'];
        $tags = $_POST['post_tags'];
        $cat = $_POST['cat'];
        // Server side validation
        if ($title == '') {
            write_here_errors()->add('title_not_vaild', __('Title not valid'));
        }
        if ($content == '') {
            write_here_errors()->add('content_not_vaild', __('Content not valid'));
        }
        if (strlen($wh_month) != 2 || strlen($wh_day) != 2 || strlen($wh_year) != 4 || strlen($wh_hour) != 2 || strlen($wh_min) != 2 || !ctype_digit($wh_month) || !ctype_digit($wh_day) || !ctype_digit($wh_year) || !ctype_digit($wh_hour) || !ctype_digit($wh_min)) {
            write_here_errors()->add('date_not_vaild', __('Date not valid'));
        } else {
            $postdate = $wh_year . '-' . $wh_month . '-' . $wh_day . ' ' . $wh_hour . ':' . $wh_min . ':' . $wh_sec;
        }
        // Add the content of the form to $post as an array
        $edit_post = array('ID' => $pid, 'post_title' => $title, 'post_content' => $content, 'post_category' => array($cat), 'tags_input' => array($tags), 'post_status' => 'publish', 'post_date' => $postdate, 'post_date_gmt' => $postdate);
        $errors = write_here_errors()->get_error_messages();
        // only create post if there are no errors
        if (empty($errors)) {
            //save the new post and return its ID
            $post_id = wp_update_post($edit_post);
            // These files need to be included as dependencies when on the front end.
            require_once ABSPATH . 'wp-admin/includes/image.php';
            require_once ABSPATH . 'wp-admin/includes/file.php';
            require_once ABSPATH . 'wp-admin/includes/media.php';
            // Let WordPress handle the upload.
            $attachment_id = media_handle_upload('wh_image_upload', $post_id);
            if (!is_wp_error($attachment_id)) {
                // If the image was uploaded successfully, set it as featured image
                set_post_thumbnail($post_id, $attachment_id);
            }
        }
    }
}