coding a WordPress AJAX Form using PHP to check if User is Logged Out and Show error

Question

I have a WordPress Frontend Form, that uses AJAX to save data. The form is processed in backend obviously using PHP post, I did a simple solution to inform form submittion errors to users if the form could not be submitted.

But My forms are specific to Logged In Users, I want my form to show an error message if WordPress user is logged out.

<form action="https://milyin.com/my/" method="post" name="milyin-add-edit" id="Add-Edit" enctype="multipart/form-data">
        <input type="hidden" name="action" value="wpfrtp_save_post">

This is the start of form, I believe that the rest of the form HTML is genuinely irrelevant to this question, so I shall not include that, incase still needed just inform me in comments, and it wont be a problem either.

here’s the JS used to Submit Form Via AJAX, and then also to show some errors.

jQuery('#Add-Edit').on('submit', function(e) {
        e.preventDefault();
        jQuery('.action-save').prop('disabled', true);
                jQuery.ajax({
                        url : jQuery(this).attr('action') || window.location.pathname,
                        type: "POST",
                        data: jQuery(this).serialize(),
                        error: function(jqXHR)  {
                                jQuery(".action-save").html("Error");
                                jQuery(".action-save").css("background-color", "red");
                                jQuery("#user_post_publish").html("Error");
                                jQuery(" #user_post_publish").css("background-color", "red");

                                msg = '';
                                if (jqXHR.status === 0) {
                                            msg = 'No Internet Connection Available. Turns out your Internet has gone to a random hibernation';
                                } else if (jqXHR.status == 404) {
                                            msg = 'Requested page not found. [404]';
                                } else if (jqXHR.status == 500) {
                                            msg = 'Internal Server Error [500]. Hold Tight, Our        Server turned Evil, we will solve it soon. Please try again after some time.';
                                } else if (exception === 'parsererror') {
                                            msg = 'Requested JSON parse failed.';
                                } else if (exception === 'timeout') {
                                            msg = 'Time out error. It took too long to transfer the data, please save again.';
                                } else if (exception === 'abort') {
                                            msg = 'Ajax request aborted.';
                                } else {
                                            msg = 'Uncaught Error.n' + jqXHR.responseText;
                                }
                                jQuery('.Error').html(msg);
                                jQuery('.Error').show();
                        }
         });

});


As you can see my in my code, in case we incur any kind of form submition error, we turn the buttons to red BG, and change there text to “Error” then we check the error code, and show a message based on the error code.

Now, if user logs out, and tries to submit form, it never says an “Error” because form submitted successfully, the AJAX seeks the success function rather than the error function.

What I am trying to do:

So, first of all, I want to ensure that if user if logged out AJAX knows its a Error in form submit, and the success() should not run, I did not include the code of success() because its lengthy and seems irrelevant.

Secondly, I want to tell the error function that the error was due to user being logged out, so that
I can inform user of it being logged out by jQuery('.Error').html(msg);

if(isset($_POST['action'])&& $_POST['action']=="wpfrtp_save_post"){
        if ( !wp_verify_nonce( $_POST['frontier_add_edit_post_'.$_POST['postid']], 'frontier_add_edit_post'  ) ){
[...]
        global $current_user;
        if(!is_user_logged in()){


//This is the problem region
// I don't understand What Should come here


        }
        
0
, , , , Aditya Agarwal 2 months 0 Answers 41 views 0

Leave an answer