0
        $filecs=get_stylesheet_directory_uri().'/css/lucilevi.css';
    $filejs=get_stylesheet_directory_uri().'/js/lucilevi.js';

    if (file_exists($filecs)) {
        echo "CS found";
    } else{
        echo "CS not found";
    }

    if (file_exists($filejs)) {
        echo "JS found";
    } else{
        echo "JS not found";
    }

Although the path for directory is correct it returns false. Can Anyone explain why this happen? I am new to PHP and Wordpress. Thanks.

1 Answer 1

1

file_exists() lets you check if a file exists on the local server, by passing it a file path. It cannot be used to check for the existence of files via URL, and you're passing it the result of get_stylesheet_directory_uri(), which returns the URL (http:// etc.) to the file, not the path.

The proper way, these days, to get the path to a theme file is to use get_theme_file_path(), like so:

$filecs = get_theme_file_path( 'css/lucilevi.css' );
$filejs = get_theme_file_path( 'js/lucilevi.js' );

if ( file_exists( $filecs ) ) {
    // etc.
}

if ( file_exists( $filejs ) ) {
    // etc.
}

Just be aware that because $filecs and $filejs are file paths, you cannot pass them to wp_enqueue_style(), wp_enqueue_script(), you still need to pass URLs to those.

1
  • Thanks for your answer Sep 24, 2019 at 1:31

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.