wp_register_script

Home » Functions » wp_register_script

WordPress is a popular content management system (CMS) that powers millions of websites on the internet. For web developers, WordPress offers a wide range of functions that can be used to customize their websites and add new features. One of these functions is wp_register_script.

The wp_register_script function is used to register a script with WordPress. This function allows developers to add custom scripts to their WordPress website or modify existing scripts. The registered scripts can be added to the website using the wp_enqueue_script function.

Here is the syntax for using wp_register_script function:

wp_register_script( $handle, $src, $deps, $ver, $in_footer );

Parameters:

  • $handle (required): A unique name for the script. It is used to reference the script later when adding it to the website.
  • $src (required): The URL of the script file.
  • $deps (optional): An array of script handles that this script depends on.
  • $ver (optional): The version number of the script.
  • $in_footer (optional): Whether the script should be loaded in the footer. By default, it’s set to false, meaning that the script is loaded in the head of the page.

Example Usage:

function my_custom_script() {
    wp_register_script( 'custom_script', 'https://example.com/js/custom_script.js', array( 'jquery' ), '1.0.0', true );
    wp_enqueue_script( 'custom_script' );
}
add_action( 'wp_enqueue_scripts', 'my_custom_script' );

In this example, we are registering a custom script called "custom_script". The script file is located at "https://example.com/js/custom_script.js". It depends on jQuery, and its version number is set to "1.0.0". Also, the script is set to be loaded in the footer of the page. Finally, we are enqueueing the script using the wp_enqueue_script function.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.