0

Here is is the call:

make_ajax_call( '<?php echo wp_create_nonce( 'tidplus-ajax-nonce' ); ?>', MyAjax.ajaxurl, 'section-frontend', 'page-ticket' );

Here is the .js code:

function make_ajax_call( nonce, ajax_url, view_to_load, in_div, param1, param2, param3, param4  ) {  
//    var ajaxurl = ajax_url;
    jQuery( '#' + in_div ).block( { message: null, overlayCSS: { backgroundColor: '#f3f4f5' } });
    jQuery.post(
        ajaxurl,
        {
            'action': 'tidplus',
            'page': view_to_load,
            'response_div': in_div,
            'task': 'load_response',
            'nonce': nonce,
            'param1': param1,
            'param2': param2,
            'param3': param3,
            'param4': param4
        },
        function ( response ) {
            setTimeout(function () {
                jQuery( '#' + in_div ).unblock();
                if ( param2 == 'append' )
                    jQuery( '#' + in_div ).append( response );
                else
                    jQuery( '#' + in_div ).html( response );
            }, 500);
        }
    )
}

It is working fine when I'm logged in, the variables are sent into the database saved and returned in form. When logged out, it will not save anything. In console appears to be an admin-ajax.php 400 bad request (but the console.log(ajaxurl) shows the correct directoty of admin-ajax.php).

protected $page_to_load;

    public static $param1;
    public static $param2;
    public static $param3;
    public static $param4;

    public function register() {
        add_action( 'wp_ajax_tidplus' , array( $this, 'post' ) );
    }

    public function post() {
        $task = sanitize_text_field( $_POST['task'] );

        if ( isset ( $_POST['page'] ) )
            $this->page_to_load = sanitize_text_field( $_POST['page'] );

        if ( isset ( $_POST['param1'] ) )
            self::$param1 = sanitize_text_field( $_POST['param1'] );

        if ( isset ( $_POST['param2'] ) )
            self::$param2 = sanitize_text_field( $_POST['param2'] );

        if ( isset ( $_POST['param3'] ) )
            self::$param3 = sanitize_text_field( $_POST['param3'] );

        if ( isset ( $_POST['param4'] ) )
            self::$param4 = sanitize_text_field( $_POST['param4'] );

        $this->handle_ajax_posts( $task );
    }

    private function verify_ajax_nonce() {
        if ( isset( $_POST['nonce'] ) ) {
            if ( wp_verify_nonce( $_POST['nonce'], 'tidplus-ajax-nonce' ) ) {
                return true;
            }
            return false;
        }
        return false;
    }

    private function handle_ajax_posts( $task ) {
        if ( $task == 'load_modal_page' )
            $this->load_modal_page();

        if ($task == 'load_response') {
            if ( $this->verify_ajax_nonce() == true ) {
                $this->load_response();
            }
        }

    }

    private function load_modal_page() {
            switch ($this->page_to_load) {
                case 'confirm-action':
                    require ( "$this->plugin_path/templates/ajax-modals/$this->page_to_load.php" );
                    break;

                case 'confirm-action-orders':
                    require ( "$this->plugin_path/templates/ajax-modals/$this->page_to_load.php" );
                    break;

                default:
                    break;
            }
        die();
    }

    private function load_response() {
        switch ($this->page_to_load) {
                case 'confirm-action':
                    require ( "$this->plugin_path/templates/ajax-modals/$this->page_to_load.php" );
                    break;

                case 'section-frontend':
                require ( "$this->plugin_path/templates/frontend/$this->page_to_load.php" );

                    break;



                default:

                    require ( "$this->plugin_path/templates/backend/tickets/$this->page_to_load.php") ;
                    break;
            }

        die();
    }
  • 1
    Please share ajax function you have created. – Bhupen Jun 27 at 12:15
  • I've added (edited) the code executed by the ajax call – dragos.nicolae Jun 27 at 12:24
2

You have not added wp_ajax_nopriv so that is the reason for ajax failure for non-logged in users.

public function register() {
    add_action( 'wp_ajax_tidplus' , array( $this, 'post' ) );
    add_action( 'wp_ajax_nopriv_tidplus' , array( $this, 'post' ) );
}

Please do this change and check.

  • Yes, that was the correct solution and now the ajax is executed without errors. However the queries are not executed but this is other story... Thank you! – dragos.nicolae Jun 27 at 12:35
  • everything works now with the same solution: add_action( 'wp_ajax_nopriv_' , array( $this, 'post' ) ); and add_action( 'wp_admin_nopriv_' , array( $this, 'post' ) ); – dragos.nicolae Jun 27 at 13:16

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.