You have to open up the place where you're generating that CSS, this could be complex or simple. It all depends on how you decide to generate your CSS based on business logic.
I have no idea how you're loading your CSS, regardless, here's how I do it - first, create your page:
public function addAdminMenuPage()
{
$this->hook_suffix = add_theme_page(
esc_html__( 'Page Title', 'my-project' ),
esc_html__( 'Page Title', 'my-project' ),
'manage_options', 'my-page-handle',
[ $this, 'myPageContent' ]
);
}
add_action( 'admin_menu', [$this, 'addAdminMenuPage' ], 80 );
Then, enqueue its CSS completely based on the suffix, making sure that your CSS only loads on that page, in the correct order:
public function generatePageCSS( $page )
{
//Very important, will only load your CSS on the right page, not in all the admin area.
if( !$page == $this->hook_suffix ) {
return;
}
//This will allow you to keep a lot of the logic in different parts.
do_action( 'page-' . $page . '-css', $page );
}
add_action( 'admin_enqueue_scripts', [$this, 'enqueuePageCSS'] );
This means that for all your CSS logic, you'll be using the page-my-page-handle-css
, it could be multiple enqueues of CSS, inline writes to the page and so on. But now you got all the CSS that's to come set in the right order, which seems to have been your original issue.
So, you're adding your page on the admin_menu
hook, then, you enqueue your scripts on the admin_enqueue_scripts
hook with checking if the generated suffix from that add_theme_page
matches the current page title that's passed down from admin_enqueue_scripts
, your "main class" to run this would be:
class MyAdminPage
{
public function __construct()
{
add_action( 'admin_menu', [$this, 'addAdminMenuPage' ], 80 );
add_action( 'admin_enqueue_scripts', [$this, 'enqueuePageCSS'] );
}
..
..
//Here you'll put the hooking functions
..
..
}
And, again, as long as you keep that generatePageCSS
"open" with hooks, probably, you're good to go.