C
C
caof192020-04-28 19:29:52
WordPress
caof19, 2020-04-28 19:29:52

Why writes that the path is not correct?

Hello, I'm making an ajax request for my site, and a problem has arisen: I have require in functions.php for my file with ajax requests

require get_template_directory() . '/inc/ajax.php';

There is one ajax request in this file, it does the following
function load_works() {
    $args = unserialize( stripslashes( base64_decode( $_POST['query'] ) ) );
    $args['paged'] = $_POST['current_page'] + 1;

    query_posts( $args );
    if ( have_posts() ) :
        while ( have_posts() ): the_post();
//                echo get_the_title();
            require '../template/items-work.php';
        endwhile;
    endif;;
    wp_die();
}

Everything seems to be correct, but in the debug log it says that the file on this path does not exist, what could be the problem?
The structure is as follows:
5ea859fc545ab091077746.png
UPD:
Given that if you move this code purely to functions, and change the path (remove "../") - everything works

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2020-04-28
@caof19

Because the path is specified not relative to the file in which the call to the require function is located, but relative to the current directory. For example, if the user called the script https://domain.name/a/b/c.php, then the current directory will be $_SERVER['DOCUMENT_ROOT'].'/a/b' and all relative paths will be built from it, regardless of the depth of nesting of included files, and not relative to included files.
If you want to specify a path relative to your file, regardless of the current directory, you can use the __DIR__ constant, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question