Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Problem with AJAX in Wordpress Plugin

Options
  • 19-02-2016 12:01pm
    #1
    Registered Users Posts: 155 ✭✭


    Hi guy's, banging my head against a wall here trying to get AJAX to work in a wordpress plugin that I am writing. Any help would be massively appreciated!

    Thanks in advance,
    Sean

    Here is where I am at -

    Below is an excerpt from my main plugin file where I register the JavaScript file I use to make the ajax call and set up my ajax handle. My understanding is the wp_localize_script should pull the PHP file I use to process the ajax request in to the wordpress fold and allow it access all the Wordpress functions. This is where I think my issue lies
    add_action( 'wp_enqueue_scripts', 'so_enqueue_scripts' );
    
    function so_enqueue_scripts(){
       wp_register_script( 'ajaxHandle', plugins_url() . '/custom-registration/js/call_ajax.js', array('jquery'), false, true );
       wp_enqueue_script( 'ajaxHandle' );
       // Localise the PHP script that will handle the AJAX request
       [COLOR="Red"]wp_localize_script( 'ajaxHandle', 'ajax_object', array( 'ajaxurl' => plugins_url( 'custom-registration/process-ajax.php' ) ) );[/COLOR]
     }
    


    Below is the JS file that makes the call -
    /custom-registration/js/call_ajax.js
    jQuery(document).ready( function($){
    	$('form.billing-form').submit(function(event) {
    	
    	event.preventDefault();
    	var data = {
    	    action:  'my_ajax_action',
    	    name:    'my name',
    	    message: 'my message'
    	}
    
    	$.ajax({
    		url: ajax_object.ajaxurl,
    		type:"POST",
    		data: data,
    		success: function( data ) {
    			alert( 'AJAX Success');
    		},
    		error: function( ) {
    			alert( 'AJAX failed');
    		}
    	})
    });
    
    


    Here is my PHP file that should process the request -
    /custom-registration/process-ajax.php
    add_action('wp_ajax_my_ajax_action', 'my_ajax_action_callback');
    function my_ajax_action_callback() {
        echo 'It works!';
        wp_die();   
    }
    

    The PHP file gets hit but a fatal error is thrown because -

    PHP LOG
    Call to undefined function add_action()
    

    add_action() is a wordpress function and my process-ajax.php mustn't be wired up correctly to access the WP functionality.


Comments

  • Registered Users Posts: 6,505 ✭✭✭daymobrew


    The WordPress Codex for "AJAX in Plugins" says to use 'ajaxurl' (not 'ajax_object' like you have).

    Change
    url: ajax_object.ajaxurl,
    to
    url: ajaxurl,


  • Registered Users Posts: 8 Weebly


    add_action() doesn't exist because you are using an external file which does not have Wordpress functions loaded into it. All AJAX requests are sent to admin-ajax.php which requires scripts designed to run on the front end to be localized.

    Easiest way to do it with an external file is to localize the script using the correct ajaxurl, point it at a function in your plugin file and require your external script there. This causes the external script to be loaded as part of the existing file and gives it access to Wordpress functions (like add_action)

    Here is a sippet to add the correct ajaxurl:

    $protocol = isset( $_SERVER )? 'https://': 'http://';

    $params = array(
    'ajaxurl' => admin_url( 'admin-ajax.php', $protocol )
    );

    wp_localize_script( 'ajaxHandle', 'ajax_object', $params );

    Then you can add your ajax action (remember to add the nopriv action also if you want logged out users to be able to use it too) pointing at a function in the same file. That function should then require or require_once your external file.

    Hope this makes sense :)


  • Registered Users Posts: 6,505 ✭✭✭daymobrew


    Weebly wrote: »
    Easiest way to do it with an external file is to localize the script using the correct ajaxurl, point it at a function in your plugin file and require your external script there. This causes the external script to be loaded as part of the existing file and gives it access to Wordpress functions (like add_action)
    It would be even simpler to put the /custom-registration/process-ajax.php file into the file that has the so_enqueue_scripts() function.


Advertisement