Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The require function cannot identify and load the php file of the current folder

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading