Naming conventions
PHP
WooCommerce core generally follows WordPress PHP naming conventions.
There are some additional conventions that apply, depending on the location of the code.
/src
Classes defined inside /src follow the PSR-4 standard. See the README for /src for more information.
The following conventions apply to this directory:
- No class name prefix is needed, as all classes in this location live within the
Automattic\WooCommercenamespace. - Classes are named using
CamelCaseconvention. - Functions are named using
snake_caseconvention. - Class file names should match the class name. They do not need a
class-prefix. - The namespace should match the directory structure.
- Hooks are prefixed with
woocommerce_. - Hooks are named using
snake_caseconvention.
For example, the class defined in src/Util/StringUtil.php should be named StringUtil and should be in the Automattic\WooCommerce\Util namespace.
/includes
The /includes directory contains legacy code that does not follow the PSR-4 standard. See the README for /includes for more information.
The following conventions apply to this directory:
- Class names are prefixed with
WC_. - Classes are named using
Upper_Snake_Caseconvention. - Functions are prefixed with
wc_. - Functions are named using
snake_caseconvention. - Hooks are prefixed with
woocommerce_. - Hooks are named using
snake_caseconvention.
Class name examples:
WC_Cache_HelperWC_Cart
Function name examples:
wc_get_product()wc_is_active_theme()
Hook name examples (actions or filters):
woocommerce_after_checkout_validationwoocommerce_get_formatted_order_total
JS
WooCommerce core follows WordPress JS naming conventions.
As with PHP, function, class, and hook names should be prefixed, but the convention for JS is slightly different.
- Global class names are prefixed with
WC. Class names exported from modules are not prefixed. - Classes are named using
UpperCamelCaseconvention. - Global function names are prefixed with
wc. Function names exported from modules are not prefixed. - Functions are named using
camelCaseconvention. - Hooks names are prefixed with
woocommerce. - Hooks are named using
camelCaseconvention.
Global class name example:
WCOrdersTable
Global function name example:
wcSettings()
Hook name example (actions or filters):
woocommerceTracksEventProperties