404 file not found when requiring PHP file

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.

Read More

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

Related posts

Leave a Reply

3 comments

  1. 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
        }
    });
    
  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' );
    
  3. 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');