0

I'm wondering what the best practice is for storing images, css, js etc in my file manager. I've come up with these 2 possible options as my preferred options.

  1. Create folders (images/, css/, js/) in the root directory

And refer to these files like <img src="/images/image1.png"> for example...

OR

  1. Create folders (images/, css/, js/) in the specific theme directory

And refer to these files like <img src="<?php bloginfo('template_directory'); ?>/images/image1.png"> for example...

I'm wondering if there is a standard or a preferred practice? I'm new to wordpress and php and would like to start off doing things correctly now, rather than trying to fix problems in the future.

Thanks!

3 Answers 3

1

Of the two option you proposed, the latter is better for maintainability.

The reason is, if you ever need to move the site to another server or use the theme on another site, it seems an easier process to move / reuse.

If you're not using the image for themeing, consider just using media library for post/page embeds.


Instead of:

bloginfo('template_directory');

Consider:

get_template_directory_uri();

or

get_stylesheet_directory_uri();

Source: https://developer.wordpress.org/reference/functions/bloginfo/#possible-values-for-show

1

In general any customization you have to wordpress core (themes, plugins, uploaded files, etc) should live under the wp-content directory. On the practical side, you should never use relative urls because you never really know where wordpress is installed, and in many naive cases they break when pretty permalink are used. In your specific case /img/... will break if wordpress is installed in a subdirectory.

Sure, if you know that your site will always be in the root of the domain, this might not be an issue, but it will limit your ability to move your site and reuse your code.

1

It depends on what you like to do really. I am saying that because the question is not very clear.

If you are talking about images you upload as part of your content (for example, a feature image of a post, or a set of images that are part of a post), then those images are handled by WordPress, and would be uploaded to the "uploads" directory. The reason for this, is if you decide to switch themes, then your images and posts are available to other themes as well.

I however think that you were probably asking about images that are part of your theme design. For example, if you are designing a theme, and part of the design contains an image, then you have to save that image within the theme itself. And as per the recommendation of WordPress, you should have an Assets directory, and inside that directory, you should have an images directory, where you save your images. You can read more details about that in the Organizing Theme Files article.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.