Support » How-To and Troubleshooting » The glob() function is looking through the WP core directory.

The glob() function is looking through the WP core directory.

  • Evening everyone,

    Have you ever tried to use the glob() function in your WordPress theme? I have and I’m trying to use it to load a bunch of .css files in my themes folder… Now, let’s view the code:

    <?php $stylesheets = glob('*.css'); ?>
    <?php foreach($stylesheets as $stylesheet) : ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/<?php echo $stylesheet; ?>" type="text/css" media="screen" />
    <?php endforeach; ?>

    If you don’t know PHP you probably can’t help. But the glob() function is supposed to get all files ending in .css (in this case) in the CURRENT directory.
    – That is the directory in which the file in which you placed the code exists. I put the code in the header.php file and hence that is the current directory.

    Now, this doesn’t work and the reason why is because the glob() function seems not to be searching the current directory but instead is searching the WP core directory, that is with all the WP core files beginning with ‘wp-‘

    But there are no .css files in the core directory and hence the code won’t work. Change it to search for .php files like so…

    <?php $stylesheets = glob("*.php"); ?>
    <?php foreach($stylesheets as $stylesheet) : ?>
    <?php print_r($stylesheet); ?>
    <link rel="stylesheet" href="/<?php echo $stylesheet; ?>" type="text/css" media="screen" />
    <?php endforeach; ?>

    And what you get out (if you view page source) is a list of all the WordPress core files AND the links to these files! Surely this is a potential security problem?

    I do not understand in any way why the glob() function is searching through the WP root directory, it SHOULD search the theme folder if it’s placed in a theme file.

    As a note, the code does work if you specify the theme directory link in the arguments like so… glob("wp-content/themes/mytheme/*.css") and it also works if you put it in its own .php file in the theme directory but this shouldn’t have to be done.

    Any and all input is appreciated, if there’s something I appear to be missing please do tell me because this is rather confusing problem I have encountered, thankyou.

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi


    Forum Moderator

    You should be enqueueing your stylesheets – not using glob().

    I’m aware of that, that’s not what I’m asking. I’m asking WHY the glob() function searches through the WP core directory instead of the theme directory.

    I could enqueue them, and I usually do but I decided to try out a different method. I could also load them using link rel=”stylesheet”… or do it numerous other ways but it doesn’t answer my question.



    Forum Moderator

    It’s for the same reason that relative links do not work predictably in WP. The template/theme system is dropped onto the content and output via the base url ( + permalink when appropriate). You’re assuming that glob("*.css") will pull all css files in the theme folder but, in reality, I think it will be trying to find something in the root domain folder and drawing a blank.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘The glob() function is looking through the WP core directory.’ is closed to new replies.