I have the following jQuery function in my custom-js.js
file:
$(function() {
$(".product-vote-button-up").click(function(){
var productID = $(this).attr('productID');
var request = $.ajax({
url: "/wordpress/wp-content/themes/my-theme/js/ajax/vote-database-update.php",
type: "POST",
data: {id : productID},
});
request.done(function() {
$("#product-" + productID + " .item-product-footer-vote-container").html('Thanks for your vote!');
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
});
});
Using AJAX, I want to update the count of my vote in my custom product database with SQL commands. To do so, I want to have a access to the WordPress function in vote-database-update.php
such as $wpdb
. The only way I managed to make it work so far, is by adding:
require_once( explode( "wp-content" , __FILE__ )[0] . "wp-load.php" );
in my PHP file. However, I think this is not really clean and after reading some of the documentation, I think there is another way around with admin-ajax.php
. In most of the documentation, it describes how to add AJAX in a plugin with admin-ajax.php
. I'm kind of confused since my vote-database-update.php
is not really a plugin (or I might not have the correct definition of a WordPress plugin). What would be the right way to include the WordPress functions in my vote-database-update.php` file?