I have a little problem with calling a file in a WordPress plugin using ajax.I have this script:
<script type="text/javascript">
function setVal()
{
var val = jQuery('#custom_text_message').val()
alert('Setting the value to "' + val + '"')
jQuery.post('session.php', {value: val})
alert('Finished setting the value')
}
jQuery(document).ready(function() {
jQuery('#custom_text_message').blur(function() {setVal()});
//setTimeout('setVal()', 3000);
});
</script>
But when this function gets called, it shows an error in the console file not found. I want to know if this is the correct way to use ajax in WordPress. If not, how can I call a file which is in the root folder of site name session.php? I’m pretty new to WordPress.
I have solve my problem on my own.First i have define ajaxurl in my themes
function.phplike below:And I put the below script on the top of my plugin file.
and here’s the field, for which i am trying to do in my plugin.
and then I put my action which i m calling to my script in
function.php.and then call my session value in to my function.That’s all i do and work for me and i hope this will helps someone else.
Note:
The
wp_ajax_your_actionaction is for admin if you need to use it on the front end the action would bewp_ajax_nopriv_your_action.