Basic guidelines to approve a WordPress theme in Themeforest Part 3

This a continuing article to “Basic guidelines to approve a WordPress theme in Themeforest Part 2”. Here we discuss more points on how to approve a theme at themeforest. This article mostly explain the JavaScript guidelines to approve the theme.

Since nowadays JavaScript is becoming crucial for many WordPress themes, we need standards and rules for it, too:

  • JavaScript code should placed in external file which can be called or include into your code.
  • Don’t use CDN to include  JS files. Please bundle them with theme.
  • JavaScript files should be enqueued using the wp_register_script then wp_enqueue_script function. Or directly using wp_enqueue_script function.
  • Inline Javascript can be add using wp_inline_script function to avoid any anomalies.
  • Don’t use the <script > and </script> into any .php files.
  • JavaScript files need to be placed in the footer where possible, barring notable exceptions, for example: Modernizr, jQuery UI, etc.
  • Don’t enqueue any pre-register JavaScript Plugins. Default Scripts Included and Registered by WordPress : https://developer.wordpress.org/reference/functions/wp_enqueue_script/
  • Also check if already enqueued before using wp_script_is function, please find the help here : https://codex.wordpress.org/Function_Reference/wp_script_is
  • All JavaScript should be written with “usestrict” mode on. For example, you can do this with jQuery as follows:
(function($){

"usestrict";

//Author code here

})(jQuery);
  • Prefer single quotes (‘) over double quotes(“), unless a string contains single quotes.
  • You’re free to use whitespace as you please in your code – don’t use whitespace on blank lines, though. And don’t forget that you’re using whitespace for readability – not for fun.
  • Name your functions and variables using camelCase, not_with_underscores. Constructors should be in Title Case.
  • You can declare multiple variables on a single line; but if you’re going to assign values, you’ll need separate lines.
  • The new Array() and new Object() notations are “no-no”s. Use shorthand equivalents ([] and {})instead.
  • Treat conditionals and loops very delicately – they’re usually the easiest to misread. Using braces and whitespace are key to this.
  • Use wp_localize_script()  to pass value to JS files from php files.
  • If you’re going to use jQuery; define an anonymous function, and pass jQuery as the argument before doing anything else. Follow this link –

https://make.wordpress.org/core/handbook/best-practices/coding-standards/javascript/#jquery