简体   繁体   English

如何使用ajax在WordPress中调用外部php文件

[英]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:我在使用 ajax 调用 WordPress 插件中的文件时遇到了一个小问题。我有这个脚本:

<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.我想知道这是否是在 WordPress 中使用 ajax 的正确方法。 If not, how can I call a file which is in the root folder of site name session.php?如果没有,如何调用位于站点名称 session.php 的根文件夹中的文件? I'm pretty new to WordPress.我对 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.在 WordPress 中,Ajax 请求应该发送到http://your-wordpress-site/wp-admin/admin-ajax.php - 可以使用admin_url( 'admin-ajax.php' ) - 你应该使用action参数指定要调用的函数。 You can pass the admin-ajax path to your javascript file via localization .您可以通过localizationadmin-ajax路径传递给您的 javascript 文件。

Add to your plugin PHP file after you enqueue your script:将脚本加入队列后,添加到您的插件 PHP 文件中:

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

In your javascript:在你的 javascript 中:

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

Function to process the ajax in your plugin PHP file:处理插件 PHP 文件中的 ajax 的函数:

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阅读更多: 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:我自己解决了我的问题。首先,我在我的主题function.php定义了 ajaxurl,如下所示:

<?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 .然后我把我调用我的脚本的动作放在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 .注意: wp_ajax_your_action操作适用于管理员,如果您需要在前端使用它,该操作将是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: WordPress 在绝对路径上工作,使用完整的 URL 而不是相对 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})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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