0

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:

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.

  • 1
    Could you elaborate a bit how this runs? This might be issue of timing or code running in different page load altogether from when you want notice to appear. – Rarst Mar 21 '14 at 21:13
  • I edited the question to explain that – IAmJulianAcosta Mar 21 '14 at 21:18
  • Is there a question here? What isn't working? – Dan Mar 21 '14 at 22:07
  • 1
    @Dan you are right. The problem is that error message isn't showing – IAmJulianAcosta Mar 21 '14 at 22:09
0

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.

  • Hi, I fixed the code using your answer, but still is not working, also I explained more in question how the plugin works – IAmJulianAcosta Mar 21 '14 at 21:25
  • @stereoactivo Is it your own profile? personal_options_update will call the function if you are editing your own profile but edit_user_profile will not: "This hook only triggers when a user is viewing another users profile page (not their own)." Add an action for show_user_profile if it's your own profile. – Tobias Mar 21 '14 at 21:34
  • I'm not editing my own profile, I'm editing subscribers profile – IAmJulianAcosta Mar 21 '14 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.