1

So I have this PHP file which has an Ajax call of a file called, let's say test.php. This test.php requires two files, this is how they're called:

require('../folder1/folder2/header.php');
require('../wp-blog-header.php');

So, if I remove the second line of code, it works just fine, but as soon as I add that second line it no longer works, the Network tab in console gives me 404.

The wordpress file and the folder1 are in the same directory.

WarSensation
  • 84
  • 2
  • 11
  • Should be `require('../wp-blog-header.php');` – Darren Jul 03 '14 at 00:52
  • My bad, it actually is require('../wp-blog-header.php'); in the original. – WarSensation Jul 03 '14 at 00:53
  • can you give me `dirname(__FILE__);` and turn on error_reporting - `error_reporting(E_ALL); ini_set('display_errors', 1);` – Darren Jul 03 '14 at 00:56
  • dirname gives me `/home/mojbrlog/public_html/esportshr/sc2/elo_league_v2.0` The `sc2` folder is where the wordpress file is in. And there are no errors reported. – WarSensation Jul 03 '14 at 01:06
  • 1
    you can add a `exit;` in the top of your `wp-blog-header.php` so see if still 404. I think the require path is not the problem. 404 response is given by `wp-blog-header.php` – Phoenix Jul 03 '14 at 02:41

3 Answers3

4

Probably you going wrong way of AJAX in wordpress. Try following code not need include any file.In functions.php file

add_action('wp_ajax_nopriv_YOUR-FUNCTION','YOUR-FUNCTION');
add_action('wp_ajax_YOUR-FUNCTION','YOUR-FUNCTION');

function YOUR-FUNCTION(){
   //  YOUR CODE
   die();
}

and client side your ajax call should following:

var ajaxurl = '<?php echo site_url();?>/wp-admin/admin-ajax.php';
$.ajax({
    type: "POST",
    url : ajaxurl,
    data : { action :'YOUR-FUNCTION',var1:your-val,var2:your-val},
    success:function(data){
        // your success call
    }
});
Abhineet Verma
  • 1,008
  • 8
  • 18
Mayur
  • 227
  • 1
  • 8
2

First, Don't use relative path(s). Check out these Questions, they're described nicely in terms of Paths.

Second, Don't use wp-blog-header.php just like that. If you really want Wordpress Environment to work inside your test.php file, Then it'll be better if you either write code as a Plugin or write your code in function.php.

Reference Links

Third, wp-blog-header.php should be called before any file you are requiring in your code. So that way Wordpress environment loads into that file as well. Otherwise, Wordpress functions will not work properly.

Check index.php inside Wordpress root folder. wp-blog-header.php is the first file required to load Wordpress Environment.

index.php (At Wordpress Root Folder)

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
Community
  • 1
  • 1
Abhineet Verma
  • 1,008
  • 8
  • 18
1

If you're receiving a 404 error it means that is not exactly a php error that is ocurring. One of this files (probably wp-blog-header.php) are trying to redirect you to another page that is no longer available (e.g: this scripts are redirecting you using an header('Location: ...') to a page that is not available).

Check the contents of the files and look for a redirection of this type. An inclusion error do not brings to you a 404 alert, it should brings a FATAL ERROR.

Additionaly...

Always when you use require() function, try to use absolute paths. Relative paths causes a lot of problems when you're using a third party code. Use

require(__DIR__.'/../wp-blog-header.php'); 

instead of

require('../wp-blog-header.php');
BigBlast
  • 397
  • 1
  • 11
  • o.O you just added path issue after my answer. Anyways, nice update. :) – Abhineet Verma Jul 03 '14 at 03:38
  • @AbhineetVerma, yes!!! I didnt know if i should make a citation so i didnt. But the objective was to improve the knowledge behind the answer, because even better then show an error to the programmer is prevent him to do it again. Thanks for your comment. ;) – BigBlast Jul 03 '14 at 03:42