00:00
00:00
Newgrounds Background Image Theme

MisterBig67425 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Ajax loading data on Wordpress

1,171 Views | 6 Replies
New Topic Respond to this Topic

Ajax loading data on Wordpress 2018-01-31 12:37:41


Hey,

I'm trying to load posts on wordpress using Ajax.

I have this in my functions.php file:

add_action ("wp_enqueue_scripts", "theme_register_scripts", 1);
function theme_register_scripts() {
    //echo get_template_directory_uri();
    wp_register_script("functions-js",esc_url(trailingslashit(get_template_directory_uri()) . "functions.js"), array("jquery"),time(),true);
    
    $php_array = array("admin_ajax" => admin_url("admin-ajax.php"));
    echo $php_array[admin_ajax];
    wp_localize_script("functions-js","php_array",$php_array);
}

add_action("wp_enqueue_scripts", "theme_enqueue_scripts");
function theme_enqueue_scripts() {
    wp_enqueue_script("functions-js");
}

add_action("wp_ajax_single", "get_single");
add_action("wp_admin_nopriv_single", "get_single");
    
function get_single() {
    global $post;
    echo $post;
    $post_id = $_REQUEST['id'];
    
    if($post_id) {
        $post = get_post($post_id);
        setup_postdata($post);
        get_template_part("content", "ajax");
        die();
    }
}

I have this in my content-ajax file (a new file I made, referenced from the code above)

<?php 
    the_title();
    the_content();
?>

I have this in a functions.js file I made

jQuery(document).ready( function($) {
    $(document).on( 'click', 'li.bullet-inline a', function( event ) {
        event.preventDefault();
        var clickedID = $(this).attr('id');
        alert("id val: " + clickedID)

        $.ajax({
            cache: false,
            timeout: 8000,
            url: $php_array.admin_ajax,
            type: 'POST',
            data: ({action:'single', id:clickedID}),
            success: function(data) {
                alert(data);
                $( '.popup' ).html(data);
                $(".popup").show();
            }
        })

    })
})

The success Ajax use to run and the code used to work, but now it does't for some reason.

The success isn't running, I believe the $php_array.admin_ajax isn't acknowledging the code set in the php file.

Response to Ajax loading data on Wordpress 2018-01-31 16:03:13


If it isn't calling your success callback then it's likely encountering an error server-side. Add an error handler (described midway down the page on the $.ajax docs) and check what it's returning. You could also take a look at your PHP error log to see what's going on.

Response to Ajax loading data on Wordpress 2018-02-01 05:05:02


At 1/31/18 04:03 PM, Diki wrote: If it isn't calling your success callback then it's likely encountering an error server-side. Add an error handler (described midway down the page on the $.ajax docs) and check what it's returning. You could also take a look at your PHP error log to see what's going on.

Thanks!

It doesn't get to the error, because it says the $php_array is undefined
ReferenceError: $php_array is not defined

Response to Ajax loading data on Wordpress 2018-02-01 12:08:23


At 2/1/18 05:05 AM, Aprime wrote:
At 1/31/18 04:03 PM, Diki wrote: If it isn't calling your success callback then it's likely encountering an error server-side. Add an error handler (described midway down the page on the $.ajax docs) and check what it's returning. You could also take a look at your PHP error log to see what's going on.
Thanks!

It doesn't get to the error, because it says the $php_array is undefined
ReferenceError: $php_array is not defined

JavaScript doesn't require variable names to start with the dollar sign like PHP does. Use php_array in your JS code instead and it should work.

Response to Ajax loading data on Wordpress 2018-02-01 14:38:18


At 1/31/18 04:03 PM, Diki wrote: If it isn't calling your success callback then it's likely encountering an error server-side. Add an error handler (described midway down the page on the $.ajax docs) and check what it's returning. You could also take a look at your PHP error log to see what's going on.
Thanks!

It doesn't get to the error, because it says the $php_array is undefined
ReferenceError: $php_array is not defined
JavaScript doesn't require variable names to start with the dollar sign like PHP does. Use php_array in your JS code instead and it should work.

Thanks! I managed to work that one out eventually, but happy you found it too!
Did you play about with it? Or reread it and saw the error?
I'm interested in how you solved it.

Response to Ajax loading data on Wordpress 2018-02-01 15:01:30


At 2/1/18 02:38 PM, Aprime wrote: Thanks! I managed to work that one out eventually, but happy you found it too!

That's good.

At 2/1/18 02:38 PM, Aprime wrote: Did you play about with it? Or reread it and saw the error?
I'm interested in how you solved it.

I don't know where this site you're working on is hosted so I was just reading the code you posted.

First, I read the docs for wp_localize_script() to figure out what that function is doing. When you posted a JS error I read your JS code more carefully and spotted the PHP-style syntax for that php_array variable. I didn't see any other errors in your JS code so I figured it had to be that.

Response to Ajax loading data on Wordpress 2018-02-01 18:16:58


At 2/1/18 03:01 PM, Diki wrote: I don't know where this site you're working on is hosted so I was just reading the code you posted.

Oh, it's local host. Thought you may just recreate it or something.

First, I read the docs for wp_localize_script() to figure out what that function is doing. When you posted a JS error I read your JS code more carefully and spotted the PHP-style syntax for that php_array variable. I didn't see any other errors in your JS code so I figured it had to be that.

That's good, you're good at analysing issues you haven't come across before.
Yeah, I was looking at it and thinking jQuery so it took a while before I realised. Always the little things xD