开发者

Wordpress and AJAX - includeing wp-blog-header breaks the call

开发者 https://www.devze.com 2023-02-04 22:18 出处:网络
I have an ajax.php file in my Wordpress themes folder, and this was working fine on the DEV server. However, when I moved it to another server, the script that I have written no longer works... bizarr

I have an ajax.php file in my Wordpress themes folder, and this was working fine on the DEV server. However, when I moved it to another server, the script that I have written no longer works... bizarre!

Im now rolling back to try and find the problem, but its most annoying because the very same version works on another server?!

Here's the code (for what its worth...)

Javascript Call:

   function change_event(ID){
     //alert("ID: "+ID);   
     $.post('wp-content/themes/muni/ajaxcalls.php', 
     { id: ID }, 
     function(data){
       alert('c开发者_运维技巧omplete: '+data);
       //$('#showingevent').html(data);
     });     
   }

ajaxcalls.php

require_once("../../../wp-blog-header.php");  
global $more; 

At this point, it breaks. There is no point including the code I have written after the require. If I comment this line, the code below works.

If I browse directly to the ajaxcalls.php file, I get the results I am expecting, but it will not feed back to the success function of the ajax call.

Any help would be greatly appreciated

Cheers SO!

Tom

edit: I'm firing change_event using this:

   $('.eventoption A').click(function(ev){
     ev.preventDefault();
     change_event($(this).attr('id'));
     clearInterval(timer);
   });

I've also updated the change_event() function to reflect the one that I'm using rather than the debug that I was using before.

The problem happens as soon as I include the wp-blog-header.php file.


The trick was to include wp-load.php instead of wp-blog-header.php


use this. It is working on localhost, and i hope it will also work for live server.

  1. First get the folder name of the root directory of your site

$folder = substr(substr($_SERVER["REQUEST_URI"],1), 0, strpos(substr($_SERVER["REQUEST_URI"],1), "/"));

  1. And then you can get the url for ajax using $_SERVER["DOCUMENT_ROOT"], $folder and wp-blog-header.php like this:

$ajax_url = realpath($_SERVER["DOCUMENT_ROOT"]).'/'.$folder.'/wp-blog-header.php';

0

精彩评论

暂无评论...
验证码 换一张
取 消