admin管理员组

文章数量:1304267

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

my-test-plugin/
├─ includes/
│  └─ dashboard.php
└─ main.php
 <?php // dashboard.php
 echo '<h1>Dashboard</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?!

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

my-test-plugin/
├─ includes/
│  └─ dashboard.php
└─ main.php
 <?php // dashboard.php
 echo '<h1>Dashboard</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?!

Share Improve this question asked Mar 2, 2021 at 6:44 goodUsergoodUser 1111 bronze badge 2
  • It could be the require_once I suppose: maybe there's something else in the code e.g. a mu-plugin added by your host that also has a require_once 'includes/dashboard.php'; meaning a different dashboard? But obviously they should be different files so not conflict. – Rup Commented Mar 2, 2021 at 10:12
  • "it doesn't work" - what happens exactly? Warning and/or fatal error? No file included? Or is the wrong file being included? Since you are passing a relative path, it's going to search the include_path - which could be different on the live site. – MrWhite Commented Mar 14, 2021 at 12:57
Add a comment  | 

1 Answer 1

Reset to default 0

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');

本文标签: plugin developmentquotdashboardquotnamed PHP file doesn39t get included