I am using module - jplayer (http://drupal.org/project/jplayer), in the jplayer.module i've found this piece of code, since i am very weak in php i need a help :)

 * Add the jPlayer library to the page.
 * @param $add
 *   By default this function will add jPlayer to the page JavaScript array
 *   directly. If wanting to store the jPlayer file as an #attached property,
 *   set this to FALSE and jplayer_add() will only return the needed array
 *   suitable for use as an #attached property.
function jplayer_add($add = TRUE) {
  static $added = FALSE;

  $directory = variable_get('jplayer_directory', 'sites/all/libraries/jplayer');
  $return = FALSE;
  if (file_exists($directory . '/jquery.jplayer.min.js')) {
    $filepath = $directory . '/jquery.jplayer.min.js';
  elseif (file_exists($directory . '/jquery.jplayer.js')) {
    $filepath = $directory . '/jquery.jplayer.js';

  if (isset($filepath)) {
    $jplayer_js = jplayer_get_file_path('jplayer.js');    
    $jplayer_css = jplayer_get_file_path('jplayer.css'); 
    $settings = array('jPlayer' => array(
      'swfPath' => base_path() . variable_get('jplayer_directory', 'sites/all/libraries/jplayer'),
      'autoPlay' => (int) variable_get('jplayer_autoplay', ''),
    if ($add) {
      if (!$added) {
        drupal_add_js($settings, 'setting');
        $added = TRUE;
    $return = array(
      'js' => array(
        array('data' => $filepath),
        array('data' => $jplayer_js),
        array('data' => $settings, 'type' => 'setting'),
      'css' => array(
        array('data' => $jplayer_css),

  return $return;

What should i modify in this code to show .js files on every page, not only when a node with a player loaded.

UPDATED. The question is - what to modify in this code to attach jplayer js files on every page, not when im viewing a node with a jquery player.

  • As a rule of thumb, you shouldn't modify third party code (Drupal core or contrib modules and themes). As it jeopardize future updates of your site. See drupal.org/best-practices/do-not-hack-core and joshuabrauer.com/2008/07/…. Commented Oct 16, 2011 at 8:36
  • i won't update this site in a future... Commented Oct 16, 2011 at 11:03
  • Unless this is a site with a very short lifespan, you (or somebody) will have to update it, if only for security updates. Call it maintenance if you like, but it will happen. Commented Oct 16, 2011 at 11:34
  • Ok, my situation is next: this module loads own .js files only when u're viewing a node with a jplayer, i'm printing out this node in a page.tpl.php, so then jplayer scripts won't loading -> player not working. Simply including jplayer.js in info file won't work. Commented Oct 16, 2011 at 11:50

2 Answers 2


The easiest way to add a JS file to your theme is to add it in the .info file:

scripts[] = js/my_script.js

Be sure to put your js file in a js folder inside your folder's theme.

Reference: Adding JavaScript to your theme or module (works in D7 too)

  • Well, i already knew about this, thanks. But then how to disable including javascripts in this module then? Commented Oct 16, 2011 at 9:20
  • I think you can use hook_js_alter() (D7) or JS Alter (D6) to remove JS added by your module.
    – tostinni
    Commented Oct 17, 2011 at 2:38

Use hook_init()

this hook is a typical place for modules to add CSS or JS that should be present on every page. This hook is not run on cached pages - though CSS or JS added this way will be present on a cached page.

  • There's no hook init functions. Commented Oct 16, 2011 at 9:57
  • No there is no hook_init() function, this is a a hook any module can implement, see drupal.org/node/292 Commented Oct 16, 2011 at 11:31
  • can u show me an example with a code in the my question? Commented Oct 16, 2011 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.