0

Alright, so I'm trying to learn how to work with AJAX in Wordpress the "right way", via admin-ajax, etc. but I'm kinda going around in circles. I want to load a portfolio item (custom post type) via Ajax - when I do it via jQuery .load() it works fine, but as soon as I try to do it the Wordpress way, it doesn't. Tutorials and StackOverflow questions have only been helpful up to a certain point... I don't know, I just feel kinda dense :-(

This is how far I've gotten, with a little (ahem) tutorial help here and there. Forgive me if there are stupid errors, for I am terrible with this kind of code. It's new to me and all. Also, thanks in advance for any help you can offer :-D

The jQuery:

jQuery(document).ready(function ($) {
    $(".getworks").click(function (event) {
        event.preventDefault();
        if (getpost !== '') {
            $.ajax({
                type: "POST",
                url: MyAjax.ajaxurl,
                data: {
                    'action': MyAjaxRequest,
                        'getpost': $(this).attr('href'),
                },
                success: function (data) {
                    // This outputs the result of the ajax request
                    $('#portfolio-back').html(data);
                    console.log(data);
                },
                error: function (errorThrown) {
                    console.log(errorThrown);
                }
            });
        }
    });
});

The PHP:

add_action( 'wp_ajax_MyAjaxRequest', 'MyAjaxFunction' );
add_action( 'wp_ajax_nopriv_MyAjaxRequest', 'MyAjaxFunction' );


function MyAjaxFunction() {

if ( isset($_POST['getpost']) ) {
    $getpost = $_POST['getpost'];

    if ( $getpost !== '' ) {

    echo $getpost;
}
}
   die();
}

UPDATE: So I added a preventDefault to the click and I $(this).attr('href') into the ajax call, and now the URL is loaded into the DIV on success, instead of the actual contents.

  • Many strange things going on... Check [this example](http://stackoverflow.com/questions/13498959/how-to-use-ajax-in-a-wordpress-shortcode/13614297#13614297). – brasofilo Mar 18 '14 at 16:36
  • Do any errors come up in the console? – MrHunter Mar 18 '14 at 18:22
  • No errors in the console. @brasofilo, thanks, I'll take a look at your example, see if I can decipher it =). – underscored Mar 19 '14 at 08:28
  • OK so I updated my question to the point where I'm at now. It seems that the jquery call is not arriving at the function. If I add a echo time to the top of the function, for instance, it doesn't do anything. There's probably something wrong in the PHP as well, but yeah... Any help would be appreciated :( – underscored Mar 21 '14 at 16:12
  • Check your network tab for the response of the ajax request - could be malformed, an error code, or something else that would get you on the right track. – jraede Mar 21 '14 at 16:30
  • Thanks @jraede, I didn't see any weirdness in the network tab, but got a little further in my quest =) See updated question. – underscored Mar 22 '14 at 10:36
  • What do you get when you `console.log(data)` – jraede Mar 23 '14 at 18:23

0 Answers0