"dashboard"-named PHP file doesn't get included

I've made a simple test plugin to demonstrate the issue:

my-test-plugin/
├─ includes/
│  └─ dashboard.php
└─ main.php
 ?php // dashboard.php
 echo 'h1Dashboard/h1';
?php // main.php
/**
* Plugin Name: My Test Plugin
*/
add_action('admin_menu', 'myTestPlugin_onAdminMenu');
function myTestPlugin_onAdminMenu() {
  add_menu_page('My Dashboard', 'My Test Plugin', 'manage_options', 'my-test-plugin', 'myTestPlugin_displayDashboardPage');
}
function myTestPlugin_displayDashboardPage() {
  require_once 'includes/dashboard.php';
}

This plugin only adds a menu item labeled My Test Plugin, that displays the dashboard page. It works perfectly on my development Windows machine; but surprisingly, it doesn't work on my online web-hosting service (LiteSpeed Web server running PHP 7.3.25, Wordpress 5.6.2)!

When I change the name of dashboard.php to anything else (e.g. dashboard1.php), it works with no problems at all! What could be the problem here and how may I fix it?!

Topic filesystem dashboard plugin-development Wordpress

Category Web


WordPress has a file with the same name at wp-admin/includes/dashboard.php. Since you are using require_once PHP will not load a file if it already has been loaded.

The fact that your setup works locally, but not on your remote server, suggests that is has something to do with PHP server set-up. Locally require_once recognizes the different paths to the two files, your web server does not. So, I would try explicitly including your plugin's directory in the call. Like this:

require_once (plugin_dir_path( __FILE__ ) . 'includes/dashboard.php');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.