0

I haven't done wordpress development in quite some time so it looks like some things have changed (naturally). I'm using codekit to compile my themes sass files and minify my scripts as well. It requires that all files have a specific output path, in other words I can't specify that the style.scss file should compile to a style.css file in the root directory. It has to go into a folder of some sort relative to the root folder, like a /css folder.

So in googling around trying to see if there was a way to tell wordpress to look into the /css folder and look for a file called site.css I found this code I can add to my functions.php file...

add_filter('stylesheet_uri','wpi_stylesheet_uri',10,2);

/**
 * wpi_stylesheet_uri
 * overwrite default theme stylesheet uri
 * filter stylesheet_uri
 * @see get_stylesheet_uri()
 */
function wpi_stylesheet_uri($stylesheet_uri, $stylesheet_dir_uri){

    return $stylesheet_dir_uri.'/css/site.css';
}

Now if I'm not mistaken it looks like I have to call that function? But when I try to call it, say in my header.php I get an error.

To summarize I am looking for the preferred way to change where wordpress looks for my themes stylesheet.

  • 2
    "To summarize I am looking for the preferred way to change where wordpress looks for my themes stylesheet." - this is a very localized concern. WordPress uses the main stylesheet for its own purposes. The Theme doesn't ever have to enqueue it at all. So, there's no real need to change where WordPress looks for it. – Chip Bennett Apr 5 '14 at 15:54
1

Create a theme's style.css as normal but just include the header information. This will be a config file only.

The register and enqueue your stylesheet(s). You can use any path you'd like. I think this is what you need:

function register_theme_styles() {
    wp_register_style( 'theme-styles', site_url( '/css/site.css' ) );
    wp_enqueue_style( 'theme-styles' );
}
add_action( 'wp_enqueue_scripts', 'register_theme_styles' );
  • Getting somewhere, now I'm getting the in error console that it's not found, but it's clearly there, it's looking for the file here... localhost:8888/project-name/css/site.css Is that correct or should it be looking to the whole relative path? – Jordan Apr 5 '14 at 15:19
  • You will need a complete URL, yes, and using localhost is only going to work if you access the site from the same machine as serves it. See: wordpress.stackexchange.com/questions/106977/… – s_ha_dum Apr 5 '14 at 15:21
  • You may also have permission issues on that directory/file. – s_ha_dum Apr 5 '14 at 15:22
  • Hmm I've never had these problems before with this setup. I'm using MAMP, is there some sort of apache or php setting that needs to be turned on or off to get localhost to work correctly? – Jordan Apr 5 '14 at 15:49
  • Any reason you're using site_url(), instead of the far more useful (and far safer) get_template_directory_uri()? By using site_url(), you need to include the path to the Theme itself, which is by default (but not always) /wp-content/themes/{theme-slug}, so your answer doesn't actually pass the correct path. site_url( '/css/style.css' ) would return example.com/{wordpress-install-directory}/css/style.css, which is not the Theme's stylesheet path. – Chip Bennett Apr 5 '14 at 15:57
0

You can use this filter, to filter out styles with any condition, or change output href string, example:

add_filter( 'style_loader_src', function($href){
if(strpos($href, "name-of-allowed.css") !== false) {
return $href;
}
return false;
});

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.