last day (14 days later) » 

9:43 AM
0
A: Unable to make a ajax call in my custom wordpress plugin

Mangesh ParteTry this: Put the ajax operation in your main plugin file add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' ); add_action( 'wp_ajax_my_action', 'my_action_callback' ); function my_action_callback() { global $wpdb; // this is how you get access to the database $id = intval(...

 
Not seems to be working! even the alert is getting fired during the call!
 
In which file did u add the PHP block?
 
In addition throwing me an error in the console panel: ReferenceError: ajax_url is not defined
 
Okay. I updated the answer. Please check.
 
I added the PHP block in my plugin file
 
9:43 AM
Check the updated answer. It's working and make sure you are getting response code '200' when you fire ajax query.
 
Now where should I define the function: my_action(), the same file where I already have the above snippet?
 
No. you don't have to define that function. It's already defined which is my_action_callback()
 
okay thats fine, so I just checked the value of the id by putting echo $id; exit; in the my_action_callback() function, but its not showing me the value
 
show me the screenshot where you have added that PHP code which I asked you to add.
 
<script>
jQuery(document).ready(function($){
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
var data = {
'action': 'my_action',
'id' : '1',
'status': '2'
};
// We can also pass the url value separately from ajaxurl for front end AJAX implementations
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
</script>
<?php
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
add_action( 'wp_ajax_my_action', 'my_action_callback' );
This is how I have done this so far
 
9:51 AM
All the code is in same file?
 
yes you right
 
That's wrong
JS code should be in your htm file
and PHP code should me in your plugin's mian file where you are defining plugin
main*
 
yes I did that
 
Whats your plugin folder name?
Suppose the name of the folder is 'xyz'
 
'easy-flexible-testimonial' it is
 
9:57 AM
then in that easy-flexible-testimonial folder you must be having one main file which must be containing structure like this:
<?php
/*
Plugin Name: Plugin Name here
Plugin URI: http://www.yourpluginurlhere.com/
Version: Current Version
Author: Name please
Description: What does your plugin do and what features does it offer...
*/
?>
In this file you will have to put that PHP block
 
yes absolutely correct, I have that
yes I did that previously
 
Okay then in one file you must be having HTML code, right?
 
yes that's right
 
In that file put the JS block
 
yes I did
 
9:59 AM
Okay, then it must work
Do you know how to check XHR requests from browser?
 
This is where I am getting redirected at the moment:
POST http://172.10.1.3:8056/wordpresstest/wp-admin/admin-ajax.php
 
Thats correct
 
Then where do I put my logic?
 
And what are the other hearder you are getting?
 
In that PHP file, I mean the main file
 
10:02 AM
You will have to put you logic in this function my_action_callback()
 
Buddy thats simple, thats what I am trying to do from the beigning, trying to echo out the varible 'id', but does not seems to be working
WIll not that show me out there?
function my_action_callback() {
global $wpdb; // this is how you get access to the database

$id = intval( $_POST['id'] );
$status = $_POST['status'];

echo $id; exit;

die(); // this is required to terminate immediately and return a proper response
}
 
Do you have timeviewer?
 
This is what I am trying to do right out there
nope pal I am in office and not allowed you know
 
you are doing it correct
 
Okay NP, let me try bit hard, hope will work out for me
Bye the where are you from?
 
10:06 AM
Put a die('hello'); above function my_action_callback() { this line
From India
Where are you from?
 
I also am from India, Nagpur
okay trying
 
Oh nagpur, Where in nagpur? I am from mumbai
 
lol!, I work here Basically I am from Odisha
 
When you refresh the page do you get blank screen with message hello
 
yes Pal I am getting that
Mangesh, you there?
 
10:17 AM
Yes, wait
my TL is here
 
okay
 
Is it working now?
 
no dude, still not
The problem is I still am unable to see the post data in console panel
 
10:41 AM
wait
 
okay
 
Do 1 thing
Cut that PHP block and paste it in functions.php file. Which u can find in your themes diretory
 
But see my problem is, the function is not callable
 
Means?
 
If thats callable what ever I echo inside it should have been out displayed
Am I right?
I mean the ajax is not able to call the fjunction I guess
*funxtion
 
10:53 AM
Yes, but show me all file code where u have that PHP block
 
okay
<?php
/*
Plugin Name: Easy Flexible Testimonial
Plugin URI:
Description:This plugin allows you add a custom testimonial section in the admin end for the user.
Author: Debakant Mohanty
Author URI: http://sharenconnect.com
Version: 1.0.0
License: GPL2 or later
*/
/* Copyright 2014 Debakant Mohanty (email : babuna.mohanty7@gmail.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
here it is
 
Everything seems correct. Do 1 thing Cut that PHP block and paste it in your functins.php file
 
let me give that a try
I put that PHP block in my functions.php file and when I refreshed the page the sql query ran and my value updated in the database
Apart from that same thing persists, not happened additionally
 
What additional thing you are talking about?
In your ajax code I can see only 1 sql query
 
yes thats it
I mean that
 
11:04 AM
I am not getting you. The code works or not?
 
no buddy, not working
 
But you said values got updated in database right? and you are supposed to cut paste entire block

add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
global $wpdb; // this is how you get access to the database
$id = intval( $_POST['id'] );
$status = $_POST['status'];
$changestatus = $wpdb->query("UPDATE wp_easy_flexible_testimonials SET is_active=0 WHERE id=1");
die(); // this is required to terminate immediately and return a proper response
 
11:27 AM
yes
 
You are missing something
 
12:03 PM
Hey you there?
My team mate is looking in to that
It was a good time with you
Catch you later buddy
 
Hey same here, thank you
Keep coding
 

last day (14 days later) »