Show error message after exception handled

I have a plugin that updates a user profile, and I want to handle a date exception and show a error message when it fails, but I can’t make it work.

Here is part of the class:

Read More
private function __construct() {
    add_action( 'edit_user_profile', array($this, "userAdminForm") );
    add_action("personal_options_update", array($this, "updateCustomFields") );
    add_action("edit_user_profile_update", array($this, "updateCustomFields") );
}
public function userAdminForm($user) {
    require_once(ACMFR_PARTIALS."/form.editProfileAdmin.php");
}

public function updateCustomFields($userID) {
    //stuff
    if ( isset($_POST["expirydate"]) ) {
        try {
            $date = new DateTime("FAIL");
            //Do stuff with Date
        }
        catch (Exception $exception) {
            add_action('edit_user_profile', array ($this, 'date_error'));
            //add_action('admin_notices', array ($this, 'date_error'));
        }
    }
    //more stuff
}

public function date_error() {
    echo '<div class="error"><p>Date Format Error</p></div>';
}

This is how the plugin works:

When admin enters the User edit profile page (user-edit.php), the plugin shows a partial with some custom fields. When the form is submitted, the updateCustomFields function is called and update the fields.

If the expirydate field has a wrong format, a Exception is raised and I want to show a error.

The problem is that the error message never appears.

Related posts

Leave a Reply

1 comment

  1. date_error seems to be a function of that class, so you’ll have to use

    add_action('edit_user_profile', array($this, 'date_error'));
    

    However I am not entirely sure that’ll work because I don’t know when this function is called. It might already be too late to register the hook.