I am making changes to an existing website that is based on WordPress (some javascript add-ins and a few PHP scripts for Ajax purposes).
Is their any proper directory that I should place all these files? I started off putting them in a folder in the root directory called assets
, but then decided maybe they should go with the rest of the WordPress template and javascripts files? Or should I keep them out of the wp-
directories, and simply keep them in the assets
folder?
I know its a trivial question, but I like doing things right- having them in directories that make sense.
You can keep all your javascript in a folder in your theme’s directory, that is
wp-content/themename/
.Concerning Ajax, it’s implementation is different in WordPress. You must add a data variable called
action
in your ajax request and then hook it to a function in yourfunctions.php
file. Your ajax url should bewp-admin/admin-ajax.php
available throughadmin_url( 'admin-ajax.php' )
in PHP.Read AJAX in Plugins for examples.
If it is presentation related I believe you should place it in the corresponding theme folder. If it is a functionality you might want consider wrapping it inside a plugin and placing it in a plugin folder.
If you dont care about abstracting this change and making available to other themes, then I would just simply add it inside the theme folder. You have WP helper functions to get the web path to the current theme folder, or to include js from that folder.
The proper place in WordPress ecosystem is the folder
wp-content
, as this is the one that you preserve while doing WP upgrades, restorations or migrations.In that folder, it could be part of a theme (
/themes
), a plugin (/plugins
), an uploaded file (/uploads
) or, if the situation requires, a custom folder (/my-custom-content
)./wp-content/themes/your-theme
Here, all presentation related code. It’s a common mistake to place general functionality code in the theme’s
functions.php
. The first question to be asked before placing custom functions in this file is:If I change my theme, will I need this function?
See: Where to put my code: plugin or functions.php?
/wp-content/plugins/your-plugin
Let’s say you need to enqueue some CSS or Javascript files, and this should happen whatever theme is being used.
The following sample plugin will load SWFObject (bundled with WP) in the page Map and the front page, as both contain a SWF Flash embed. And, in the rest of the site, it will load the WebFont Loader from Google CDN and a CSS file from within the plugin folder.
/wp-content/uploads
Here, your theme or plugin should place all user uploaded content, so it can be managed through WP Media Library screen. And the content should survive any theme swap or plugin de-activation.
/wp-content/custom-folder
Many image gallery plugins use this approach to store their custom media library.
Another use is for Mobile themes, where custom user themes are placed in this folder, so not to lose it on plugin update (as everything in the plugin or theme folders is replaced on upgrades).