Question

Update: Error was a simple typo, disregard this question.

I'm using the jQuery validate plugin to validate some forms. When I try to validate something using the is_user_logged_in() function, I get the following error in PHP:

PHP Fatal error: Uncaught Error: Call to undefined function is_user_loggged_in()

I know this is an issue with WordPress load order, but I'm not sure what the best solution would be.

My code looks like this. A class with a validation function inside, which is called from the wp_ajax hooks.

// Class containing validation function
class NS_User_System_Password_Settings {    

    // ...

    public function validate_current_password( $current_password = NULL ) {
        // Check if logged in
        if ( !is_user_loggged_in() ) {
            return $this->get_error( 'not_logged_in' );
        }

        // Rest of validation...
    }

    // ...
}

// Action hook for AJAX validation
add_action( 'wp_ajax_ns_account_settings_current_password_validation_callback', 'ns_account_settings_current_password_validation_callback' );
add_action( 'wp_ajax_nopriv_ns_account_settings_current_password_validation_callback', 'ns_account_settings_current_password_validation_callback' );
function ns_account_settings_current_password_validation_callback() {
    $validate_current_password = NS_User_System_Password_Settings()->validate_current_password( $_POST['current_password'] );

    if ( is_wp_error( $validate_current_password ) ) {
        echo json_encode( $validate_current_password->get_message() );
    } else {        
        echo json_encode( true );
    }

    wp_die();
}

And in jQuery I call the validation action like so:

$('#account-settings-password-form').validate({
    submitHandler: function(form) {
        $.ajax({
            url: ajax_url,
            type: 'POST',
            data: $(form).serialize(),
            success: function(response) {
                // ...              
            },
            complete: function() {                      
                // ...
            }
        });
    },  
    rules: {
        current_password: {
            required: true,
            minlength: 6,
            maxlength: 60,
            remote: {
                url: ajax_url,
                type: 'post',
                data: {
                    'action': 'ns_account_settings_current_password_validation_callback',
                    'current_password': function() {
                        return $('#account-current-password').val();
                    }
                }
            }   
        },
    }
});
Was it helpful?

Solution

The answer is right in your question and in the error message.

Take a look at this part of your code:

public function validate_current_password( $current_password = NULL ) {
    // Check if logged in
    if ( !is_user_loggged_in() ) {
        return $this->get_error( 'not_logged_in' );
    }

    // Rest of validation...
}

You have a typo in there. You use is_user_loggged_in() and there really is no such function. It should be is_user_logged_in() (only two 'g's).

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top