I've started creating a WP plugin which should work only in Admin Area.
I've managed to create top-level menu with required sub-menu items, but when I try to print content of my page, it prints it twice, in a global scope of Admin panel and inside the page itself.
Main plugin file loads menu settings and separate page classes: require_once('settings.php');
require_once('pages/VmSettingsPage.php');
require_once('pages/VmTradersPage.php');
$settingsPage = new VmSettingsPage();
$tradersPage = new VmTradersPage();
Menu settings:
add_action('admin_menu', 'cvmAdminMenu');
function cvmAdminMenu() {
add_menu_page('VM Tool', 'VM Tool',
'manage_options', 'camden-vmtool', ['VmSettingsPage','pageInit']);
add_submenu_page('camden-vmtool', 'Traders Pages', 'Traders Pages', 'manage_options',
'camden-vmtool-traders', ['VmTradersPage','pageInit']);
add_submenu_page('camden-vmtool', 'Style Pages', 'Style Pages', 'manage_options',
'camden-vmtool-style', 'cvmRenderStyle');
add_submenu_page('camden-vmtool', 'Cat Navs', 'Cat Navs', 'manage_options',
'camden-vmtool-cat-navs', 'cvmRenderCatnavs');
}
Settings Page class:
<?php
class VmSettingsPage {
private $options;
public function __construct()
{
add_action('admin_init', [$this, 'pageInit']);
}
public function pageInit() {
echo 23123;
}
}
Question #1 - The above code will output 23123 twice, when visiting plugin page and when not. How do I make it print the content of my page only when page is the current screen? Basically: http://localhost/wp-admin/admin.php?page=camden-vmtool
Question #2 - How to preload only one page class based on currently opened page? I will have around 4 pages, and It feels bad to load classes at once, just to have the class instance initiated.
BTW, get_current_screen's base parameter != $_GET['page'], so I cannot even match those to print correct content.
Thanks