Got custom CSS and JavaScript running in your WordPress plugin? Let’s make sure that WordPress only applies the custom styles and JS to your admin pages. It’s a good practice to limit where resources load to make sure that WordPress sites stay fast and secure.

Here are two quick ways to check for the page names.

As an example my admin page url is be https://morgan.com/wp-admin/admin.php?page=morgan-settings

 function morgan_enqueue_scripts($hook) {
    if ( $hook = 'morgan-settings'  ) {  // the page name
 
       wp_enqueue_style('morgan-css', plugins_url('includes/morgan.css',__FILE__ ));
       wp_enqueue_script('morgan-js', plugins_url('includes/morgan.js',__FILE__ ));

     }
    }

    add_action('admin_enqueue_scripts', 'morgan_enqueue_scripts');

The second way to check for the admin page and enqeue scripts looks like this

    function morgan_enqueue_scripts() {
    if(isset( $_GET['page'] ) && $_GET['page'] == 'morgan-settings' ) {
 
       wp_enqueue_style('morgan-css', plugins_url('includes/morgan.css',__FILE__ ));
       wp_enqueue_script('morgan-js', plugins_url('includes/morgan.js',__FILE__ ));

     }
    }

    add_action('admin_enqueue_scripts', 'morgan_enqueue_scripts');

If someone knows the exact pros/cons of each method please drop a comment. Otherwise if you used the code drop a comment as well ? Hope it helped make your plugins run better!