doulu6234 2018-06-18 16:56
浏览 85
已采纳

Ajax调用包括在Wordpress中不起作用

I have a PHP file that I call with Cron Job and I need it separately from the rest of code. So, I have a button that call AJAX request from my dashboard. The Ajax function in my Wordpress plugin includes the file (process.php), this in turn, includes another file (snippets.php).

If I call process.php directly, it works perfectly, but not from Ajax call.

Folder structure

plugin.php
plugin.js
snippets.php
processes/process.php

plugin.js

jQuery.ajax({
type: 'GET',
url: ajaxurl,
data : {
action : 'process',
}
})

plugin.php

function process(){
include_once( 'processes/process.php' );
}
add_action( 'wp_ajax_process', 'process' );

process.php

include_once('../snippets.php')
//here not includes 'snippets.php'
  • 写回答

1条回答 默认 最新

  • douhan8430 2018-06-18 17:21
    关注

    You should use relative includes from their own file, otherwise, it will run from the current path. To force this you can use __DIR__ and start with a slash

    plugin.php

    function process(){
    include_once(__DIR__.'/processes/process.php' );
    }
    add_action('wp_ajax_process', 'process' );
    

    process.php

    include_once(__DIR__.'/../snippets.php');
    //here not includes 'snippets.php'
    

    Also, you forgot the semicolumn ; in process.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FileNotFoundError 解决方案
  • ¥15 uniapp实现如下图的图表功能
  • ¥15 u-subsection如何修改相邻两个节点样式
  • ¥30 vs2010开发 WFP(windows filtering platform)
  • ¥15 服务端控制goose报文控制块的发布问题
  • ¥15 学习指导与未来导向啊
  • ¥15 求多普勒频移瞬时表达式
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标