Site logo

Archived topic

Custom stylesheet, where to place it?

7 replies · Started by Jeffrey on July 4, 2018

Viewing posts 1–8 of 8

I had a custom stylesheet I load to style my login screen. I created a directory called "custom" under .../wp-content/themes/generatepress/

and that is where the stylesheet was loaded from. However on the last update of theme, that directory got wiped and I had to restore the "custom" directory and .css file from backup.

So what I am asking is there a safe place to put the stylesheet so it won't get wiped out with theme updated. I know something about child themes but did not want to go that route necessarily, I just like having the stylesheet that just serves this one dedicated purpose. I believe I could just use an absolute path under /wp-content, just not sure about best practices or if it matters.

So as it is the function looks like:

function custom_login_stylesheet() {
wp_enqueue_style( 'custom-login', get_stylesheet_directory_uri() . '/custom/adx_custom_login.css' );
}
add_action( 'login_enqueue_scripts', 'custom_login_stylesheet' );

Hi there,

This is actually exactly what child themes are made for. It would prevent you from losing the changes when you update the parent theme.

Alternatively, you could build a custom plugin using a plugin like this: https://wordpress.org/plugins/pluginception/

You would need to replace get_stylesheet_directory_uri() with plugin_dir_url( __FILE__ ).

Ok thanks Tom. Yeah regarding the child theme, I kind of get that aspect, but at the same time, this stylesheet really has nothing to do with the theme. I guess I was just wondering if there was a best practice for locating or placing some custom stylesheets on a WP installation, looking for a safe spot where they would be secure and not get overwritten on updates. You are suggesting in the plugins area and just make a plugin for it, I'll look into it, but trying to avoid plugin proliferation. But I could move the stylesheet there (plugins directory) and avoid an actual plugin, right? Seems like a simple enough task but trying to avoid tripping up over something.

I suppose you could do that, but you would still need to add the function to enqueue the file. I suppose you could use a plugin like Code Snippets, but you're still using a plugin then.

Code is code, whether it's a plugin, in a child theme or in the parent theme. There's no difference performance-wise between any of those methods.

I am already using snippets for a number of things.

In that case you can upload the file anywhere on your server outside of a theme or plugin that receives updates.

Then use Code Snippets to enqueue the file.

function custom_login_stylesheet() {
    wp_enqueue_style( 'custom-login', 'URL TO FILE HERE' );
}
add_action( 'login_enqueue_scripts', 'custom_login_stylesheet' );

Gotcha, thanks!

You're welcome! :)

This archived topic is closed to new replies.