$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - is_user_logged_in() not working in AJAX validation call|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - is_user_logged_in() not working in AJAX validation call

matteradmin9PV0评论

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();
                    }
                }
            }   
        },
    }
});

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();
                    }
                }
            }   
        },
    }
});
Share Improve this question edited Feb 18, 2019 at 16:50 Swen asked Feb 18, 2019 at 16:38 SwenSwen 1,4047 gold badges22 silver badges37 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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).

Post a comment

comment list (0)

  1. No comments so far