require_once Error with Relative Path during WPCode Snippet Validation
-
Report:
require_onceError with Relative Path during WPCode Snippet ValidationHi,
We have identified that WPCode snippet validation fails if arequire_oncestatement uses a relative path and is executed during the save/activation process.1. Error DetailsThe error is the classic PHP
Failed opening required file.php, which is thrown when the include instruction is reached.2. Demonstration (Inconsistent Validation Context)Tests demonstrate that the failure is dependent on the execution of the
require_once, suggesting that the Current Working Directory (CWD) during validation is not the WordPress root ($\text{ABSPATH}$):Execution of require_once ‘dir/file.php’Condition/Variable SetResult (WPCode Validation)Yes (Executed)Outside Condition, or Condition is TRUEFAILURE ❌No (Skipped)Condition is FALSESUCCESS ✅3. ConclusionThe WPCode validation environment does not resolve relative paths correctly. To ensure activation and reliability, we are forced to use absolute paths based on WordPress constants:
PHP
require_once ABSPATH . 'dir/file.php';// TEST
// (run in snippet not applied to page)
// Does NOT activate in WPCode -> error
require_once 'dir/file.php';
// Does NOT activate in WPCode -> error
if(empty($associato)){
require_once 'dir/file.php';
}
// ACTIVATES in WPCode -> no error
if(!empty($associato)){
require_once 'dir/file.php';
}
// ACTIVATES in WPCode -> no error
$associato = '123';
if(empty($associato)){
require_once 'dir/file.php';
}
// Does NOT activate in WPCode -> error
$associato = '123';
if(!empty($associato)){
require_once 'dir/file.php';
}Behavior can lead to ambiguous results.
Thank you for your attention.
You must be logged in to reply to this topic.