Skip to content

Instantly share code, notes, and snippets.

@craigmdennis
Last active August 29, 2015 14:02
Show Gist options
  • Save craigmdennis/7d8a30d83f6452e71520 to your computer and use it in GitHub Desktop.
Save craigmdennis/7d8a30d83f6452e71520 to your computer and use it in GitHub Desktop.
List contents of a directory with a specific filetype. Used in developing Wordpress with Grunt.
<?php
// In case the list_directory.php is in a subfolder
// we need to know the base directory of the theme folder
// on the server.
define('THEME_PATH', dirname(__FILE__) );
?>
<?php
<head>
<!-- Load stuff for development -->
<?php if (WP_ENV == 'local') : ?>
<?php list_directory( '.tmp/styles', '.css'); // Load developement styles ?>
<!-- Load development version of modernizr -->
<script src="<?php bloginfo('template_directory'); ?>/app/bower_components/modernizr/modernizr.js"></script>
<!-- Load stuff for production (minified + concatenated) -->
<?php else : ?>
<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
<script src="<?php echo bloginfo('template_directory'); ?>/assets/scripts/modernizr-custom.js"></script>
<?php endif; ?>
</head>
?>
<?php
// ToDo: Add exceptions param
function list_directory( $directory, $filetype ) {
// Get only the filetypes we want
$dir = THEME_PATH . '/' . $directory;
$files = glob( $dir . '/*' . $filetype);
// Check to make sure the directory actually exists
if ( file_exists( $dir ) ) {
foreach ( $files as $file) {
// Store the filename
$filename = basename( $file );
// Match the file type an do special things
switch ($filetype) {
// For stylesheets
case '.css':
echo '<link rel="stylesheet" href="' . get_bloginfo('template_directory') . '/' . $directory . '/' . $filename . '"/>';
break;
// For scripts
case '.js':
echo '<script src="' . get_bloginfo('template_directory') . '/' . $directory . '/' . $filename . '"></script>';
break;
} // END switch
} // END foreach
} // END if directory exists
} // END list_directory
?>
<?php
// Check to see if a local config file exists
if ( file_exists( $domain_dir . '/local-config.php') ) {
// If it does exist, then use it
include( $domain_dir . '/local-config.php');
// Environment variable
define('WP_ENV', 'local');
// For developers: WordPress debugging mode.
define('WP_DEBUG', true);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment