Support » Developing with WordPress » Nonce creates a 403 error

  • montanan

    (@montanan)


    I created a plugin with this code and it works fine. I am creating a second plugin and it is giving me a “the server responded with a status of 403 (Forbidden)” Error. I used the code from the WordPress.org Plugin Developer Handbook.

    add_action('wp_enqueue_scripts', 'FMS_enqueue_scripts');
    function FMS_enqueue_scripts() {  // this function initialized scripts
       
        wp_enqueue_script( 'my_javascript_js', plugins_url( '/js/my_javascript.js', __FILE__ ), array('jquery'), '1.0', $in_footer = true);
    
        wp_enqueue_style( 'my_css', plugins_url( '/css/my-css.min.css' , __FILE__ ),$deps = array(), '1.0');
    
        $fmn = wp_create_nonce( '34234356' );
    
        wp_localize_script( 'my_javascript_js', 'my_ajax_obj', array(
           'ajax_url' => admin_url( 'admin-ajax.php' ),
           'nonce'    => $fmn,
        ) );
       
    
    }
    
    add_action( 'wp_ajax_nopriv_msc-find-product', 'MSC_FMS_ajax_handler' );
    
    add_action( 'wp_ajax_msc-find-product', 'MSC_FMS_ajax_handler' );
    
    function MSC_FMS_ajax_handler() { // this function gets the ajax data
      
        check_ajax_referer( '34234356'); // checks for the correct nonce
      
        if(array_key_exists('shed', $_POST) ) {
           
            print_r($_POST);
                 
        }
        else {
             echo '#';
        }
        
        wp_die(); // All ajax handlers die when finished
    }
    

    Here is the Javascript

    jQuery(document).ready(function () {
    
        jQuery("#storage-form").on("submit", function () {
    
            var shedInfo = 'good';
    
            jQuery.post(my_ajax_obj.ajax_url, { //POST request
    
                _ajax_nonce: my_ajax_obj.nonce, //nonce
    
                action: "msc-find-product", //action
    
                shed: shedInfo //data
    
            }, function (data) { //callback
    
                console.log(data);
                //window.location.href = data;
    
                return false;
    
            });
    
            return false;
        });
    
    }) // end ready
    • This topic was modified 1 month ago by bcworkz. Reason: unresolved OP resolved in error
Viewing 1 replies (of 1 total)
  • montanan

    (@montanan)

    It is conflicting with my other nonce.

    Here is the javascript for Plugin One
    _fmp_nonce: my_ajax_obj.nonce, //nonce
    Javascript for Plugin Two
    _msp_nonce: my_ajax_obj.nonce, //nonce

    They both work fine as long as one plugin is not activated or if I comment out the check_ajax_referer() line.

    • This reply was modified 1 month ago by bcworkz. Reason: consolidate topics
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.