Your problem is with the path you’re using to include the file.
plugin_dir_path()
just gets the directory of the given file/directory with a trailing slash. So if your code is here:
plugin/admin/views/key-analyzer-groups.php
Then dirname( __FILE__ )
will return
plugin/admin/views
So plugin_dir_path( dirname( __FILE__ ) )
will return
plugin/admin/
Which means that you are trying to include this path:
plugin/admin/admin/templates/groups.php
Which doesn’t exist.
If you want a reliable way to get the directory for your plugin to use throughout your code, then what you should do is define a function in your root plugin file that returns the path, then use that function throughout your code:
function key_analyzer_plugin_path() {
return plugin_dir_path( __FILE__ );
}
Then use that function in your other files to include:
class Key_Analyzer_Groups {
public static function init() {
include_once key_analyzer_plugin_path() . 'admin/templates/groups.php';
}
}
You could also tweak this approach to create a function for including files inside your plugin:
function key_analyzer_include( $file ) {
$file = ltrim( $file, "https://wordpress.stackexchange.com/" );
include plugin_dir_path( __FILE__ ) . $file;
}
Now you can use that function to include all your files by passing it a path relative to the main plugin file:
class Key_Analyzer_Groups {
public static function init() {
key_analyzer_include( 'admin/templates/groups.php' );
}
}