2

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.

1
  • You can provide the full URL in your jQuery.post() or use /session.php Commented Feb 18, 2013 at 7:22

4 Answers 4

1

I have solve my problem on my own.First i have define ajaxurl in my themes function.php like below:

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

And I put the below script on the top of my plugin file.

<script type="text/javascript">
function setVal()
{
    var val = jQuery('#custom_text_message').val()
    var data = {
        action: 'my_action',
        value: val,
    };

    jQuery.post(ajaxurl, data, function(response) {
       alert('Got this from the server: ' + response);
    });
}

jQuery(document).ready(function() {
    jQuery('#custom_text_message').blur(function() {setVal()});
    //setTimeout('setVal()', 3000);
});
</script>

and here's the field, for which i am trying to do in my plugin.

<textarea name="custom_text_message"  id="custom_text_message"></textarea>

and then I put my action which i m calling to my script in function.php.

add_action('wp_ajax_my_action', 'my_action_session');

function my_action_session() {
    global $wpdb; // this is how you get access to the database

    session_start();

    $_SESSION['cus_msg'] = $_POST['value'];

    die(); // this is required to return a proper result
} 

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_action action is for admin if you need to use it on the front end the action would be wp_ajax_nopriv_your_action.

Sign up to request clarification or add additional context in comments.

Comments

1

In WordPress, Ajax requests should be made to http://your-wordpress-site/wp-admin/admin-ajax.php - which can be obtained using admin_url( 'admin-ajax.php' ) - and you should use action parameter to specify which function to call. You can pass the admin-ajax path to your javascript file via localization.

Add to your plugin PHP file after you enqueue your script:

 wp_localize_script( 'your-script', 'js_obj', array('ajax_url' => admin_url( 'admin-ajax.php' ) );

In your javascript:

jQuery.post(js_obj.ajax_url, {value: val, action: 'run-my-ajax'})

Function to process the ajax in your plugin PHP file:

function call_my_ajax(){
  // do stuff
  exit;
}

add_action('wp_ajax_run-my-ajax', 'call_my_ajax');
add_action('wp_ajax_nopriv_run-my-ajax', 'call_my_ajax');

Read more: https://codex.wordpress.org/AJAX_in_Plugins

5 Comments

Where i have to define this action, For now i have put this action to my theme function.php but when i check this server response is 0.can you suggest what i m doing wrong?
Totally normal for it to return 0 on success. You might want to just echo values and exit at the end of the function.
Even if i echo and exit my value from function ,nothing happen just return a response 0;
Did you check the response on the network tab in Firebug(or equivalent) ?
Check the link in the question, the examples are pretty straightforward. Add all PHP codes to functions.php. Call all javascript codes using wp_register_script and wp_enqueue_script
0
<script type="text/javascript">
function setVal()
{
    jQuery.ajax({url:"<?php bloginfo('url');?>/wp-content/plugins/plugin_folder_name/session.php",success:function(result){alert(result);}}
}
</script>  

Comments

0

WordPress works on absolute paths, use a complete URL instead of the relative URL:

jQuery.post('session.php', {value: val})

Use something like:

jQuery.post('<?php bloginfo('url');?>/wp-content/plugins/plugin_folder_name/session.php', {value: val})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.