Cleggy
Cleggy

Reputation: 725

Inserting a JavaScript reference into all WordPress pages

We have a WordPress MU install (v2.6.2), and I'm trying to figure out how to include a reference to a JavaScript file into all pages served. Research shows me that the key to this is to use wp_enqueue_scripts, but I don't know where to place the call to this.

I think I can achieve what I want by adding the call to header.php for every theme. But is there a single place where I can do this, so I don't have to update each themes header.php file?

I don't require the JavaScript to be included into the admin pages, but it doesn't matter if the solution results in this happening.

Upvotes: 0

Views: 122

Answers (1)

BigBagel
BigBagel

Reputation: 860

WordPress MU was integrated into WordPress itself in version 3.0. The current version as of this post is 3.5.1. I can tell you how to do it in the current version but it might need tweaking to get it working in the version your running.

This would place a script in the header:

<?php
function my_scripts_method() {
    wp_enqueue_script(
        'any-name-you-want',
        'url/of/script'
    );
}    

add_action('wp_enqueue_scripts', 'my_scripts_method');
?>

Here's the codex page for wp_enqueue_script()

*Note: Look into these functions when building the URL to your JS file.

You can either add this to every theme's functions.php file or save it as a PHP file and use it as a must use plugin. If you want to make it a must use plugin, place it in the wp-content/mu-plugins directory and it will run automatically for all sites. In a more current version of WordPress you could also make it a normal plugin and network activate it, but I don't think that option is available in your version.

With regards to any issues you may encounter related to the version you're running:

I know wp_enqueue_script() was added in 2.6, but I'm not sure about the corresponding wp_enqueue_scripts hook. If it doesn't work you can try hooking into init:

add_action('init', 'my_scripts_method');

You might even get away with a naked call to wp_enqueue_script() without hooking it into an action if init doesn't work either. If you have to go with either of these, you should surround it with the following conditional to keep it from being added to your admin screens:

if ( ! is_admin() ) {
    /* code */
}

Honestly, you really should consider upgrading. I know it can be a pain, but there are multiple known security issues in older versions of WordPress. Not to mention some pretty awesome new features.

Upvotes: 1

Related Questions