2

I am developing plugin wherein I want to add bootstrap.css to plugin setting page. But if I add css, it will get add to whole WordPress admin panel.

Is there any way to apply css to particular plugin only?

1 Answer 1

0

You can use get_current_screen() to check which page is being displayed now, and if it was your plugin's page, enqueue the script.

However the easier way is to use the global variable, $pagenow:

if(( $pagenow == 'my-plugin.php' ) {
    wp_enqueue_style('my-style', 'URL-HERE' );    
}

In which my-plugin.php is the slug of your plugin screen's URL, such as:

www.example.com/wp-admin/my-plugin.php
3
  • Hi Jack, Thanks ! it works for me. But just queries, as it is on page level, we don't have something on plugin level ? because if I want to do it for multiple pages, then I need to add "&&" in if statement like if(( $pagenow == 'my-plugin.php' && $pagenow == 'my-settings.php' ) something like this. Commented Aug 1, 2017 at 5:24
  • Yes you can do that too, it's the correct way. You can also use get_the_current_screen();, it's a much more powerful function, that also allow queries such as plugin.php?query=sample.
    – Johansson
    Commented Aug 1, 2017 at 5:47
  • @JackJohansson the function name is get_current_screen (not get_the_current_screen)
    – hbit
    Commented Jan 14, 2020 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.