0

My question may be strange, but it really confused me!

In my child theme, I used to call and load the files next to the function.php file without any problem, but after deleting and re-inserting all the files in the child folder, it is not possible to identify the files. I used the following commands.

Inside the function.php file, I call its side file, the user_function.php file, but it is not recognized even with the command

if (file_exists('user_function.php')) {
     require_once('user_function.php');
} else {
     echo "Please try back in 30 minutes...\n";
     die;
}

The file is not recognized! and displays the error message!!!

Of course, my site is loaded, but when I go to wp-admin, it is clear that the user_function.php file is not loaded.

I have only these two files in my child theme folder:

  • function.php
  • user_function.php

Where is the problem from? (These instructions are not really complicated or difficult and I have used these instructions many times before in different places, but now they are challenging me)

I switched several times between other themes and even the parent template

+update:

I ran into a strange problem! Now, every other file that I want to load into function.php and user_function.php, I have to address it with __DIR__, I think the problem must be somewhere else! I sent a hosting ticket, they say that a problem happened a few hours ago and the connection with the database was disconnected, and they did not explain what happened, but I think that this must have caused the problem and damaged my WordPress path or addresses. Is it possible that the problem is from Did this happen?

SubZer0
  • 65
  • 6
  • 1
    Please use the following code to see what folder it addresses: foreach (scandir('.') as $file) echo $file. "\n"; – Adam Luper Jul 29 '23 at 17:06
  • It displays these files!!! /// about.php admin-ajax.php admin-footer.php admin-functions.php admin-header.php admin-post.php admin.php async-upload.php authorize-application.php comment.php credits.php and... – SubZer0 Jul 29 '23 at 17:09
  • I don't have these files in the child theme folder or even the parent theme! – SubZer0 Jul 29 '23 at 17:10
  • Welcome to Stack Overflow. You have discovered that there is a working directory. See https://www.php.net/getcwd and https://en.wikipedia.org/wiki/Working_directory . Additionally consult your operating systems user and administration manual to lean about the basic properties of a process (standard streams, command line, working directory and environment). PHP runs as a process and executes the PHP code you write. – hakre Jul 29 '23 at 18:00
  • Does this answer your question? [relative path in require\_once doesn't work](https://stackoverflow.com/questions/5371828/relative-path-in-require-once-doesnt-work) – A.L Jul 29 '23 at 21:43

1 Answers1

0

It may be due to the fact that the function.php is called from another file in another directory.

In order to avoid ambiguities, you can use a full path by providing __DIR__, that returns the directory of the current file, so that PHP will search for the user_function.php file in the same directory than the function.php file:

// in function.php
if (file_exists(__DIR__.'/user_function.php')) {
     require_once(__DIR__.'/user_function.php');
} else {
     echo "Please try back in 30 minutes...\n";
     die;
}
A.L
  • 10,259
  • 10
  • 67
  • 98
  • Thanks, can I find the file that causes this to happen? Why didn't this happen before? Could it be a problem with hosting?! – SubZer0 Jul 29 '23 at 17:33
  • I ran into a strange problem! Now, every other file that I want to load into function.php and user_function.php, I have to address it with __DIR__, I think the problem must be somewhere else! I sent a hosting ticket, they say that a problem happened a few hours ago and the connection with the database was disconnected, and they did not explain what happened, but I think that this must have caused the problem and damaged my WordPress path or addresses. Is it possible that the problem is from Did this happen? – SubZer0 Jul 29 '23 at 17:50
  • 1
    This must not be a problem. Quite the opposite, it is generally recommended to use `__DIR__`. – hakre Jul 29 '23 at 18:02