Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Plugin Building WordPress Widgets, and Shortcodes Adding AJAX To Plugins on the Front-End: Part 2

I don't see the <script> var ajaxurl </script> in my source code? Does this work with Wordpress 4.4?

function wptreehouse_badges_refresh_profile() {
    $options = get_option( 'wptreehouse_badges' );
    $last_updated = $options['last_updated'];

    $current_time = time();

    $update_diff = $current_time - $last_update;
    if( $update_diff >= 30 ){
        $wptreehouse_username = $options['wptreehouse_username'];
        $options['wptreehouse_profile'] = wptreehouse_badges_get_profile( $wptreehouse_username );
        $options['last_updated'] = time();

        update_option( 'wptreehouse_badges', $options );
    }
    die();
}
add_action( 'wp_ajax_wptreehouse_badges_refresh_profile', 'wptreehouse_badges_refresh_profile' );

function wptreehouse_badges_enable_frontend_ajax(){
    //instruction to wordpress that when an ajax command is submitted - pass it through admin and down to the function
?>
    <script>
        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
    </script>
<?php
}
add_action( 'wp-head', 'wptreehouse_badges_enable_frontend_ajax' );
jQuery(document).ready(function($){

    $('.wptreehouse-badge').hover(function() {
        $(this).find('.wptreehouse-badge-info').stop(true, true).fadeIn(200);
    }, function() {
        $(this).find('.wptreehouse-badge-info').stop(true, true).fadeOut(200);
    });

    $.post(ajaxurl, {
        action: 'wptreehouse_badges_refresh_profile'
    }, function( response ) {
        console.log( 'AJAX complete' );
    });

});

Fixed needed to use 'wp_head' hyphen to underscore and break out the $url

function wptreehouse_badges_enable_frontend_ajax(){
    //instruction to wordpress that when an ajax command is submitted - pass it through admin and down to the function
    $url =  admin_url('admin-ajax.php');
?>
    <script>
        var ajaxurl = '<?php echo $url; ?>';
    </script>
<?php
}
add_action( 'wp_head', 'wptreehouse_badges_enable_frontend_ajax' );