简体   繁体   中英

how to call external php file in WordPress using ajax

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.

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

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 im 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 .

<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>  

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})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM