patrick
patrick

Reputation: 657

Wordpress Enqueue Js scripts

I am having trouble getting wp_enqueue functions to work. I've looked at all the documentation on it but am having trouble sifting through and finding out what is supposed to go where.

so far I understand that I am supposed to register and enqueue the files from the functions.php file of the theme I am creating. So that is exactly what I do. I create some PHP tags and drop it in the middle of them, at the bottom of the page. Save and Upload.

When I reload, it just returns a blank white screen, must be an error in the code or something.

Here is the function:

<?php
function add_scripts(){
wp_register_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
wp_register_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
wp_register_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
wp_register_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
wp_register_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
wp_register_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
wp_register_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
wp_register_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js');
wp_enqueue_script('jquery');
wp_enqueue_script('nivo');
wp_enqueue_script('slimbox');
wp_enqueue_script('picasa');
wp_enqueue_script('pwi')
wp_enqueue_script('swf');
wp_enqueue_script('simpletube')
wp_enqueue_script('jqvalidate');
}

add_action('init','add_scripts');
?>

So is there some sort of problem with my syntax? I'm not that strong with PHP. Any help is greatly appreciated. Thanks!

Upvotes: 1

Views: 1000

Answers (3)

Brickr
Brickr

Reputation: 134

Instead of your code I would use:

<?php
function add_scripts(){
  wp_enqueue_script('jquery', 'http://code.jquery.com/jquery-1.5.2.min.js');
  wp_enqueue_script('nivo', get_bloginfo('url').'/scripts/nivo.js');
  wp_enqueue_script('slimbox',get_bloginfo('url').'/scripts/slimbox2.js');
  wp_enqueue_script('picasa', get_bloginfo('url').'/scripts/jquery.EmbedPicasaGallery.js');
  wp_enqueue_script('pwi',get_bloginfo('url').'/jquery.pwi-min.js');
  wp_enqueue_script('swf', get_bloginfo('url').'/jquery.swfobject.1-1-1.min.js');
  wp_enqueue_script('simpletube',get_bloginfo('url').'/scripts/jquery.simpletube.js');
  wp_enqueue_script('jqvalidate', get_bloginfo('url').'/jquery.jqvalidate.js'); 
}

add_action('wp_enqueue_scripts', 'add_scripts');

So please notice I have removed "wp_register_script" as using that is totally unnecessary if you are going to call wp_enqueue immediately after register.

wp_register_script

Is used so that you can afterwards call it ANYWHERE else in code without including the path.

Also big change is that I'm not calling the function from

init

But I'm calling it from

wp_enqueue_scripts

Also please consider adding additional parameters to your wp_enqueue_script such as

wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )

Upvotes: 0

RichardTape
RichardTape

Reputation: 21713

It's kind of hard to debug it without seeing the whole file but the fact you get a 'blank page' suggests there's definitely something larger than a syntax problem somewhere.

Do you definitely have correctly nested php tags? i.e.

<?php

some code

<?php

some more code

?>

some more code

?>

will give you problems.

Also, it's now common practice to leave the last ?> from the end of the file (it means you wont have any issues with having whitespace after the closing tags and they're not necessary)

On top of that, you've used wp_register_script('jquery'...) - WordPress already has jquery registered. If you wish to re-register it, you need to wp_deregister_script('jquery') first. I'd also only do that outside of the admin, so:

if(!is_admin()){wp_deregister_script('jquery'); <your wp_register_script stuff> }

If these things don't help, copy and paste your entire functions.php file (use pastebin.com and give us a link)

As an aside, you're using get_bloginfo('url') several times - which means you're running lots of unnecessary calls to the database. Stick it into a variable and save yourself a little overhead:

$my_url = get_bloginfo('wpurl');

wp_register_script('thing', $my_url.'/script/location/file.js');

Oh! One more thing, I don't think url is an allowed argument for get_bloginfo() I think you want wpurl

Codex page on get_bloginfo() function

Good luck!

Upvotes: 2

stealthyninja
stealthyninja

Reputation: 10371

Missing ; for the following two lines:

wp_enqueue_script('pwi')
wp_enqueue_script('simpletube')

Upvotes: 1

Related Questions