Déjà vu
Déjà vu

Reputation: 783

Wordpress can´t find file

I hope my title isn´t too ´general´ but I hope someone here can help me.

Since this was my time working with WordPress i´ve stumbled upon a lot of issues but after looking at all the premade functions I was able to solve them eventually. However now I can´t seem to solve it.

I want to know if a file exists.

So basically I did this:

$filename = IMG_PATH.'exampleImage.png';

  if(file_exists($filename)){
      echo 'The file '. $filename .' exists';
  }else{
      echo 'The file '. $filename .' does not exist';
  }

when I run this it says path/to/image/exampleImage.png does not exist.

Alright maybe the path is wrong or something. So to test I added:

<?php echo '<img src="'.IMG_PATH.'exampleImage.png">'; ?>

beneath my if statement and ran the code. The image shows just fine in my browser.

This means that IMG_PATH.'exampleImage.png' exists however in my IF i can't seem the find it.

Am I being stupid right now and made a super stupid mistake or what happened? I hope a fresh pair of eyes can solve my problem. Thanks!

EDIT:

Alright since the path from IMG_PATH seems to be relative as people said. Let me add another example.

Why can't i include the javascript file in my header?

Right now i have the whole javascript placed in the header. But since this WordPress theme has it's own folder of javascript files I decided to place it in there. Let's say it's named jsFile.js.

It uses the JS_PATH to find it's path to the JS folder. Similar as IMG_PATH.

When I try to include it like:

<?php echo '<script src="'.JS_PATH.'jsFile.js"></script>'; ?>

It just doesn't work. But when I just do:

<?php readfile(JS_PATH.'jsFile.js'); ?>

it will show everything that's inside of the jsFile.js as plain text meaning the path is correct.


echoing IMG_PATH: http://[websitehere]/wp-content/themes/[themehere]/img/

Upvotes: 0

Views: 1062

Answers (1)

gantners
gantners

Reputation: 471

Following the comments above, an example for adding js files to a child theme, taken from wordpress reference

readfile reads the content of the file, that is what you do not want, instead the server wants to parse and interpret your js file in order to build up the html markup for your website.

Make your js files available to the theme using the enqueue function of wordpress:

<?php
    function my_scripts_method() {
    wp_enqueue_script(
        'custom_script',
        get_template_directory_uri() . '/js/jsFile.js',
        array('jquery')
    );
   }
   add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

You can modify the js path as you wish.

Upvotes: 1

Related Questions