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 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路