0

I've been browsing through similar questions and have yet to get this functionality to work. I'm tracking the progress a user makes an embedded vimeo video and insert data or update data.

JS on the post page:

        $.ajax({
        type: 'POST',
        dataType: 'json',
        url: ajaxurl,
        data: { 
            'action': 'vimeo_progress',
            'progress_percent': progress,
            'progress_seconds': seconds,
            'course_id': courseID },
        success : function(data) {
            console.log(data); //FOR DEBUG
        },
        error : function(data, textStatus, XMLHttpRequest) {
            console.log('updateProgress failed!'); //FOR DEBUG
            console.log(textStatus); //FOR DEBUG
        }
    });

functions.php

    add_action( 'wp_ajax_nopriv_vimeo_progress', 'store_vimeo_progress_callback' );
function store_vimeo_progress_callback() {  
    // don't track if user is not logged in
    if ( ! is_user_logged_in() )
        return false;

    $user_id = get_current_user_id();
    $vimeo_percent = sanitize_text_field($_POST['progress_percent']);
    $vimeo_seconds = sanitize_text_field($_POST['progress_seconds']);
    $xxx_course_id = sanitize_text_field($_POST['course_id']);

    $table_name = $wpdb->prefix . "course_video_progress";

    // Run WP query to retrieve user progress
    $row = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table_name WHERE user_id = %d AND course_id = %d;", $user_id, $xxx_course_id) );

    if ($row) {
        $wpdb->replace( 
            $table_name, 
            array( 
                'id' => $row->id,
                'user_id' => $user_id, 
                'course_id' => $xxx_course_id,
                'progress_percent' => $vimeo_percent,
                'seconds_played' => $vimeo_seconds
            ), 
            array( 
                '%d',
                '%d',
                '%f', 
                '%d' 
            ) 
        );
    } else {
        $wpdb->insert( 
            $table_name, 
            array( 
                'user_id' => $user_id, 
                'course_id' => $xxx_course_id,
                'progress_percent' => $vimeo_percent,
                'seconds_played' => $vimeo_seconds
            ), 
            array( 
                '%d',
                '%d',
                '%f', 
                '%d' 
            ) 
        );
    }

    $response = array( 'success' => true, 'data' => 'hello' ); //if $data is set
    wp_send_json_success( $response );
    exit;
}

No data is ever inserted into table and no data is ever updated. I do get a "success" from callback but the data is returned as 0.

edit: I just realized http://localhost/wordpress/wp-admin/admin-ajax.php returns 0 so maybe the ajax call is not even finding my function and simply returning 0?

1

I'm not entirely sure you want to call exit at the end of that function, since exit terminates all further PHP execution. This is likely the cause of your troubles.

http://php.net/manual/en/function.exit.php

That said, you can always test for the result of your queries by setting a variable to be the result, and then sending that as your $response.

if ($row) {
    $qry_result = $wpdb->replace( 
        $table_name, 
        array( 
            'id' => $row->id,
            'user_id' => $user_id, 
            'course_id' => $xxx_course_id,
            'progress_percent' => $vimeo_percent,
            'seconds_played' => $vimeo_seconds
        ), 
        array( 
            '%d',
            '%d',
            '%f', 
            '%d' 
        ) 
    );
} else {
    $qry_result = $wpdb->insert( 
        $table_name, 
        array( 
            'user_id' => $user_id, 
            'course_id' => $xxx_course_id,
            'progress_percent' => $vimeo_percent,
            'seconds_played' => $vimeo_seconds
        ), 
        array( 
            '%d',
            '%d',
            '%f', 
            '%d' 
        ) 
    );
}

if( false !== $qry_result ) {
    $qry_result = true;
}

$response = array( 'success' => $qry_result, 'data' => 'hello' ); //if $data is set
wp_send_json_success( $response );
  • See the example on the Codex for an AJAX callback: codex.wordpress.org/AJAX_in_Plugins It reccomends calling wp_die() to terminate immediately and return a proper response – kingkool68 Mar 15 '15 at 4:15
  • I think the guidance on that page is misinformed and can easily cause unexpected issues. wp_die() doc codex.wordpress.org/Function_Reference/wp_die notes it should be used sparingly. If this is the only code block in your functions.php / plugin, then maybe it's OK to call wp_die; but I would omit it, especially if you have other stuff in that plugin / functions.php file. – user12479 Mar 16 '15 at 14:11
  • It's a response fro man AJAX request. You don't need the script to continue. That's why you call wp_die(), die(), or exit. If you don't WordPress appends a '1' to the end of the response. – kingkool68 Mar 16 '15 at 15:50
1

If you omit the datatype parameter from your AJAX call in your JS section of code jQuery will attempt to determine the datatype of the response from the server. While you're debugging this can be helpful as you can simply echo values from your AJAX callback in PHP. You can read the results in your console.log() statements.

An alternative is to enable the WP_DEBUG_LOG constant in your wp-config.php file. Then you can call the error_log() function in PHP which will log the response to the debug.log file in your /wp-content/folder of your site. See http://codex.wordpress.org/Debugging_in_WordPress#WP_DEBUG_LOG

Those two tips should help you debug what is going on with your AJAX callback.

0

Another tip I would give is to make use of wp_send_json_error(). https://codex.wordpress.org/Function_Reference/wp_send_json_error

That way you can pass messages if things go wrong. And these json messages will show up when you hit the endpoint directly. For example, if you have a user permissions check, if they do not have permission output wp_send_json_error('you do not have permission to do this') Or something like that.

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.