0

Once my_file.php is loaded through Ajax I can't get any WordPress content from it, even though Wordpress functions run without generating any error messages. Note that I'm doing Ajax the old school way (the new regular way to do Ajax with WordPress is too complex for me).

JS:

var root_path;
var host_address = document.location.hostname;              
if (host_address == '127.0.0.1') { 
    root_path = 'http://127.0.0.1/wordpress/my_project';
    var local_server = 1; 
}
else {
    root_path = host_address;   
    var local_server = '';  
}   
var x = 'wp-content/themes/my_project/my_file.php';
if (local_server == 1) x = root_path + '/' + x;             
else x = 'http://' + root_path + '/' + x;   

$.ajax({
    type: "post",
    url: x,
    data: $(this).serialize(),      
    success: function(msg){
        $( '.container' ).html(msg);
    }
});     

Then in my_file.php:

require_once( "../../../wp-config.php" );

... which gives me access to WP functionality. After that, get_categories() returns nothing but "uncategorized" and query_posts() returns nothing at all (the exact same calls return plenty of categories and posts from non Ajax pages)

$args = array(
    'orderby' => 'name',
    'parent' => 0
);
$categories = get_categories( $args ); 

//

query_posts(array ( 'posts_per_page' => -1 ));
while ( have_posts() ) : the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;
  • Any errors with WP_DEBUG enabled? PS but really really I would recommend to invest a bit of time into not-that-new-anymore Ajax in WP core. :) – Rarst Nov 23 '15 at 22:34
1

The correct answer is to just do AJAX the right way.

Enqueue your script and localize it to add the path to admin-ajax.php:

function wpd209588(){
    wp_enqueue_script(
        'wpd209588_script',
        get_template_directory_uri() . '/js/your-script.js?ver=1.0',
        array( 'jquery' )
    );
    wp_localize_script(
        'wpd209588_script',
        'WPaAjax',
        array(
            'ajaxurl' => admin_url( 'admin-ajax.php' )
        )
    );
}
add_action( 'wp_enqueue_scripts', 'wpd209588_scripts' );

In your-script.js, do your AJAX call:

jQuery(document).ready(function($){
    $.post(
        WPaAjax.ajaxurl,
        {
            action : 'wpd209588_ajax'
        },
        function( response ){
            $( '.container' ).html( response );
        }
    );
});

Map your AJAX handler to the action you call in your AJAX script:

function wpd209588_ajax_function(){
    global $wp_query;
    $args = array(
        'posts_per_page' => -1
    );
    $wp_query = new WP_Query( $args );
    while ( have_posts() ) : the_post();
        echo '<li>';
        the_title();
        echo '</li>';
    endwhile;
    exit;
}
add_action( 'wp_ajax_wpd209588_ajax', 'wpd209588_ajax_function' );
add_action( 'wp_ajax_nopriv_wpd209588_ajax', 'wpd209588_ajax_function' );
  • Thanks a lot! You just made me switch to the right way and for good :) So nowadays the code of the pages loaded through Ajax isn't stored any longer in separate files but are all in functions.php? – drake035 Nov 25 '15 at 18:58

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.