Inside a function I’m doing something like this:

$optional_css_exists = get_template_directory_uri() . "/css/optional.css";
  if ( file_exists($optional_css_exists) ) {
      wp_enqueue_style('options', get_template_directory_uri() . '/css/optional.css', 'style');
  }

But for some reason it’s not being enqueued. When I echo out $optional_css_exists, I get the correct path of the file.

If I remove the if statement, the file is enqueued.

Why isn’t the file being enqueued when it exists and the path is correct?

1 Answer
1

URL path can’t be passed to file_exists

PHP: file_exists – Manual

Use get_template_directory() or get_stylesheet_directory() instead:

if(file_exists(get_template_directory()./path/file.css'))

as that returns server path

Leave a Reply

Your email address will not be published. Required fields are marked *