In the plugin, The css file loads sitewide by default, I want to load it only on the Single post.

the conditional tags, i.e, is_single() isn’t working in plugins,

any idea how can we achieve that?

  • Can you share with us your code, like what did you try so far? – Ismail Jun 1 '16 at 0:16
  • Thank you so much for the help. So, basically there is a plugin called TablePress and its only used in single post so it doesn't make sense to load the css sitewide. I tried using this code pastebin.com/jLT0t8P6 but it didn't worked all the way through. I will appreciate any help. – Ahmed Kaludi Jun 1 '16 at 0:45
  • This code checks whether on home page or not, you should be using wp_enqueue_script hook with wp_enqueue_style to add the CSS or wp_head for inline ones, and there use if( ! is_single() ) return; on top so as to avoid queuing the CSS while not needed.. What's confusing? – Ismail Jun 1 '16 at 1:39


You are hooking early.

In the plugin, the css file loads site-wide by default, I want to load it only on the single post.

It's a very good thought many developers don't take this into account while developing themes/plugins.

is_single() isn’t working in plugins

Where are you using is_single template tag in WP run cycle ?

Assuming you are hooking to init or wp

Template tags are not available yet until template_redirect action. So those tags don't work for init or wp.

Best place to work with scripts or styles is on wp_enqueue_scripts which runs after template_redirect .

Any idea how can we achieve that ?

There are many ways to achieve depending on the plugin we are using.

Coming to TablePress plugin.

controllers/controller-frontend.php is the place where plugin is en-queue-ing stylesheet.

if ( apply_filters( 'tablepress_use_default_css', true ) || TablePress::$model_options->get( 'use_custom_css' ) ) {
    add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_css' ) );

You can completely turn off en-queuing the stylesheet using the tablepress_use_default_css and custom css option to false, and then load them wherever you need in your custom plugin/theme.

If you dig through the file there are many conditional en-queuing is done for that plugin.

You can also dequeue style first and then enqueue wherever needed.

Sample code

function wp_228402_load_styles_conditionally() {
    return; // return if not on the single post/page

Check the priority for the above that's the key it should be greater than default value 10.

  • For conditionals like is_single() then, how then do we tell our plugin "Wait until wp or init is loaded and then run all of this plugin's functions"? – Nathan Apr 7 '19 at 16:50

You need something like this in your functions.php file:

if( is_single()) {
  wp_enqueue_style( 'ahmedkaludi-style', get_stylesheet_uri() . '/css/plugin-styles.css' );

Change '/css/plugin-styles.css' to the actual path of the plugin's stylesheet.

If the plugin is written well, wp_enqueue_style will be wrapped in a function, like:

if ( ! function_exists( 'my_scripts' ) ) {
  function my_scripts() {
    wp_enqueue_style( 'ahmedkaludi-style', get_stylesheet_uri() . '/css/plugin-styles.css' );
  add_action( 'wp_enqueue_scripts', 'steveclason_flat_scripts' );

If that's how it's done in the plugin you can override it with a similar block of code in your theme's functions.php. We can't make very good recommendations without seeing how the plugin calls the stylesheet -- the block of code on your Pastebin does something else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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