bbp_enqueue_script

Enqueue a script from the highest priority location in the template stack.

Description

(string) bbp_enqueue_script( (string) $handle = '', (string) $file = '', (array) $dependencies = array(), (constant) $version = false, (string) $in_footer = 'all' ); 

Registers the style if file provided (does NOT overwrite) and enqueues.

Returns (string)

The script filename if one is located.

Parameters (5)

0. $handle — Optional. (string) => ''
Name of the script.
1. $file — Optional. (string) => ''
Relative path to the script. Example: /js/myscript.js..
2. $dependencies — Optional. (array) => array()
An array of registered handles this script depends on. Default empty array.
3. $version — Optional. (constant) => false
String specifying the script version number, if it has one. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the script.
4. $in_footer — Optional. (string) => 'all'
Whether to enqueue the script before or before. Default false.. Accepts false. or true .

Usage

  1. if ( !function_exists( 'bbp_enqueue_script' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/core/template-functions.php'; 
  3.  
  4. // Name of the script. 
  5. $handle = ''; 
  6.  
  7. // Relative path to the script. Example: '/js/myscript.js'. 
  8. $file = ''; 
  9.  
  10. // An array of registered handles this script depends on. Default empty array. 
  11. $dependencies = array(); 
  12. $version = false; 
  13.  
  14. // Optional. Whether to enqueue the script before or before . 
  15. // Default 'false'. Accepts 'false' or 'true'. 
  16. $in_footer = 'all'; 
  17.  
  18. // NOTICE! Understand what this does before running. 
  19. $result = bbp_enqueue_script($handle, $file, $dependencies, $version, $in_footer); 
  20.  

Defined (1)

The function is defined in the following location(s).

/includes/core/template-functions.php  
  1. function bbp_enqueue_script( $handle = '', $file = '', $dependencies = array(), $version = false, $in_footer = 'all' ) { 
  2.  
  3. // No file found yet 
  4. $located = false; 
  5.  
  6. // Trim off any slashes from the template name 
  7. $file = ltrim( $file, '/' ); 
  8.  
  9. // Make sure there is always a version 
  10. if ( empty( $version ) ) { 
  11. $version = bbp_get_version(); 
  12.  
  13. // Loop through template stack 
  14. foreach ( (array) bbp_get_template_stack() as $template_location ) { 
  15.  
  16. // Continue if $template_location is empty 
  17. if ( empty( $template_location ) ) { 
  18. continue; 
  19.  
  20. // Check child theme first 
  21. if ( file_exists( trailingslashit( $template_location ) . $file ) ) { 
  22. $located = trailingslashit( $template_location ) . $file; 
  23. break; 
  24.  
  25. // Enqueue if located 
  26. if ( !empty( $located ) ) { 
  27.  
  28. $content_dir = constant( 'WP_CONTENT_DIR' ); 
  29.  
  30. // IIS (Windows) here 
  31. // Replace back slashes with forward slash 
  32. if ( strpos( $located, '\\' ) !== false ) { 
  33. $located = str_replace( '\\', '/', $located ); 
  34. $content_dir = str_replace( '\\', '/', $content_dir ); 
  35.  
  36. // Make path to file relative to site URL 
  37. $located = str_replace( $content_dir, content_url(), $located ); 
  38.  
  39. // Enqueue the style 
  40. wp_enqueue_script( $handle, $located, $dependencies, $version, $in_footer ); 
  41.  
  42. return $located;