Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add ajax to wordpress theme

I've got a problem that I'm stuck on for days now... I'm trying to use a simple ajaxPOST function to send data to a MySQL database (not WP database).

This code is located within "single-post.php" in the theme, because it must be checked before every post.

$.ajax({ url: 'library/functions/admin_checkuser.php',
     data: {action: userID},
     type: 'post',
     success: function(output) {
                  alert(output);
     }
});

I'm simply sending a variable to a "admin_checkuser.php" script which in turn calls another script that take actions on the database.

This is the code for "admin_checkuser":

$userid = $_POST['action'];

echo $userid;//for testing
$oMySQL = new MySQL();
$query = "Select * FROM videotable WHERE uid = '$userid'";
$oMySQL->ExecuteSQL($query);
$bb = $oMySQL->iRecords;
$aa = $oMySQL->aResult;
echo $bb;

if ($bb == 0){
$query = "INSERT INTO videotable VALUES ('','$userid','true')";
$oMySQL->ExecuteSQL($query);
echo 'true';
    exit();

}else{
$sharing = mysql_result($aa,0,"share");
echo $sharing;
exit();

}

But I don't think the calls go through to the script. These scripts where tested outside of WordPress and did work, so it must be something in WordPress that blocking the ajax call. BTW, I tried placing the "admin_checkuser.php" in many diffrent folders but nothing worked.

Thanks in advance.

like image 238
user1108310 Avatar asked May 14 '12 18:05

user1108310


People also ask

Can I use AJAX in WordPress?

You can see “admin-ajax. It was initially created for all the functions that make AJAX requests from the WordPress admin. It is also used for the public part of the web. All WordPress AJAX requests must go through a PHP script. In other words, admin-ajax.

How do I get AJAX data in WordPress?

In WordPress, we send all AJAX request to a common URL, then wordpress internally calls the corresponding method according to the parameters which we have sent with the request. You can use the admin_url( 'admin-ajax. php' ) function of WordPress to get this url.

Can you use AJAX with PHP?

Start Using AJAX Today In our PHP tutorial, we will demonstrate how AJAX can update parts of a web page, without reloading the whole page. The server script will be written in PHP. If you want to learn more about AJAX, visit our AJAX tutorial.


2 Answers

You're much better off using the inbuilt Wordpress AJAX request.

So in your themes functions.php file, add your function to be called, for example:

function checkUser() {

    $userid = $_POST['user']; //validation also :)
    $oMySQL = new MySQL();
    $query = "Select * FROM videotable WHERE uid = '$userid'";
    $oMySQL->ExecuteSQL($query);
    $bb = $oMySQL->iRecords;
    $aa = $oMySQL->aResult;
    echo $bb;

    if ($bb == 0){
    $query = "INSERT INTO videotable VALUES ('','$userid','true')";
    $oMySQL->ExecuteSQL($query);
    echo 'true';
        exit();

    } else {
    $sharing = mysql_result($aa,0,"share");
    echo $sharing;
    exit();

    }
}

After that, you add your hook with connects to the inbuilt AJAX System

add_action('wp_ajax_check_user', 'checkUser');
add_action('wp_ajax_nopriv_check_user', 'checkUser');

wp_ajax_nopriv_%s allows it to be called from the front end.

And then, in your javascript file, you just fire off your ajax request.

$.post(ajaxurl, { action: 'check_user', user: userId }, function(output) {
     alert(output);
 });

If ajaxurl is undefined, you will need to create it in your template file, something like this should work, but there are other ways.

add_action('wp_head','ajaxurl');
function ajaxurl() {
?>
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
<?php
}

Backend of wordpress does the rest.

It takes the action passed in the AJAX request, looks for the corresponding `wp_ajax(_nopriv)_%s hook and then calls the function that is assigned to the hook.

It will also pass in either the $_POST or $_GET depending on the type of AJAX request.

You can read a little more about using AJAX inside of Wordpress.

like image 192
Stoosh Avatar answered Sep 23 '22 16:09

Stoosh


You should check your url for your ajax call.

Maybe use the full url instead of the relative one.

It could be because of the location of your theme makes the url incorrect. I'm assuming your ajax code is in your theme folder.

There are some wordpress functions that get the theme directory.

For example if you at this page http://yourwebpage/test/ then the ajax call will go here http://yourwebpage/test/library/functions/admin_checkuser.php. This I assume would be the incorrect location.

This is why you need to add the absolute url to your script. And if it is in your theme, you can use this method get_template_directory_uri() to get the template directory.

See here: http://codex.wordpress.org/Function_Reference/get_template_directory_uri

like image 25
Gohn67 Avatar answered Sep 24 '22 16:09

Gohn67