• Hi,

    I’m new to WP and I need some help with a situation. I’m developing an ‘help’ site with documentation to an external platform.

    The main goal is to have the Help Site but also load pieces of content in the platform, through AJAX requests. For example, in users section load the users related post/page.

    I’ve been looking for examples and found:
    . DOING_AJAX
    . wp_ajax_($action)
    . wp_ajax_nopriv_($action)
    . wp_doing_ajax()

    I’ve built a small plugin with this only to test:

    <?php
    var_dump(wp_doing_ajax());
    ?>

    And added a button on footer to make an AJAX request. In both situations I get the same message:
    /wp-content/plugins/ajax-call/ajax-call.php:18:boolean false

    AJAX request:

    $('#button').click(function(){
    		$.post(
    			'/', 
    			{
    					'action': 'add_foobar',
    					'data':   'foobarid'
    			}, 
    			function(response){
    				console.log(response);
    			}
    		);
    	})

    Any idea on what am I doing wrong? Any suggestions or directions that I should take?

    Would it be too wrong to use $_SERVER keys to detect an AJAX/external request?

    Many thanks,
    mrocha

Viewing 1 replies (of 1 total)
  • 1. you must determine ajax action something like this

    
    add_action( 'wp_ajax_'.'my_action', 'my_action' );
    add_action( 'wp_ajax_nopriv_'.'my_action', 'my_action' );
    

    2 determine function my_action

    
    function my_action(){
    echo "this is result";
    die;
    }
    

    3. then make ajax request pass like parameter action = my_action

    
    $.ajax({
     url: my_object.ajax_url,
     type: 'POST',
     data: {
       action: 'my_action',
       data: 'something',
     },
     success: function(msg){
       console.log(msg);
     },
    });
    

    my_object.ajax_url this is you must pass variable to your javascript script or determine explicitly like https://yourSiteName/wp-admin/admin-ajax.php.

    instead this

    
    url: my_object.ajax_url,
    

    add this

    
    url: 'https://yourSiteName/wp-admin/admin-ajax.php',
    

    or add code that will add variable to you javascript file using this code

    
    add_action( 'wp_enqueue_scripts', 'includeJavaScriptPublic' );
    function includeJavaScriptPublic(){
     wp_enqueue_script( 'your-js-script', plugins_url('/path/to/your/script', dirname(__FILE__)), array('jquery'), '1.0.0', true);
     wp_localize_script( 
       'your-js-script', 
       'my_object', 
        array(
         'ajax_url'=>admin_url('admin-ajax.php'), 
        ) 
     );
    }
    

    read better this documentations link

    I think you need to spend a few days and then you will know how to do it. It’s not easy for newbie, I’d spent a few day to understand how it works, and not so far don’t know how it works. always forget something…

    • This reply was modified 6 years, 8 months ago by neo332.
    • This reply was modified 6 years, 8 months ago by neo332.
    • This reply was modified 6 years, 8 months ago by neo332.
    • This reply was modified 6 years, 8 months ago by neo332.
Viewing 1 replies (of 1 total)
  • The topic ‘External AJAX request’ is closed to new replies.