I would approach this in different way.
In this original index.php file, keep it simple, like this:
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'My_Plugin' ) ) {
require_once __DIR__ . '/includes/class-my-plugin.php';
$my_plugin = My_Plugin::get_instance();
}
In this new file you create class:
class My_Plugin {
protected static $instance = null;
protected $basedir = null;
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
public function get_basedir() {
return $this->basedir;
}
protected function __construct() {
$this->basedir = dirname( dirname( __FILE__ ) );
// Here put your hooks
}
}
With this basic construct, you can use hooks to create your plugin features.
If you want to make page for your plugin features, do not create post with post type page, but create config page using add_menu_page
https://developer.wordpress.org/reference/functions/add_menu_page/
It will be visible in menu on the left in wp-admin.
It can be done like this:
add_action( 'admin_menu', array( $this, 'register_settings_page' ) );
This code put in __constuct, admin_menu is a hook name, and array( $this, ‘register_settings_page’ ) points that function that should be called is inside this class file, and function name is “register_settings_page”
And create function like this:
public function register_settings_page() {
add_menu_page(
'My Plugin',
'My Plugin',
'manage_options',
'surfer',
array( $this, 'settings_page' )
);
}
Description of all params you can find in documentation:
https://developer.wordpress.org/reference/functions/add_menu_page/
And again you create function “settings_page” where you can build content:
Like this:
public function settings_page() {
require_once My_Plugin::get_instance()->get_basedir() . '/templates/settings.php';
}
With this build, you should have this structure:
- index.php
- includes
- - class-my-plugin.php
- templates
- - settings.php