I decided to try to create a plugin that calls forth a couple of javascript files and a css file using the WordPress Plugin Generator.
It is working somewhat – it’s calling one of the javascript files but not the other two.
I am using the wp_enqueue_script function, but probably wrong.
Any help would be greatly appreciated!
<?php
/*
Plugin Name: Tab Header Code
Plugin URI: [insert the plugin uri here]
Description: Inserts appropriate javascript and css libraries for tabs
Author: Jesse Wallace
Version: 0.1
Author URI: http://www.enticent.com
Generated At: www.wp-fun.co.uk;
*/
if (!class_exists('tabstyles')) {
class tabstyles {
/**
* PHP 4 Compatible Constructor
*/
function tabstyles(){$this->__construct();}
/**
* PHP 5 Constructor
*/
function __construct(){
add_action("init", array(&$this,"add_script1"));
add_action("init", array(&$this,"add_script2"));
add_action("init", array(&$this,"add_script3"));
add_action("wp_head", array(&$this,"add_css"));
}
/**
* Tells WordPress to load the scripts
*/
function add_script1(){
wp_enqueue_script('tab_header_code_script1', '/wp-content/plugins/tab-header-code/js/tabview-min.js', NULL , 0.1);
}
function add_script2(){
wp_enqueue_script('tab_header_code_script2', '/wp-content/plugins/tab-header-code/js/element-min.js', NULL , 0.1);
}
function add_script3(){
wp_enqueue_script('tab_header_code_script3', '/wp-content/plugins/tab-header-code/js/yahoo-dom-event.js', NULL , 0.1);
}
/**
* Adds a link to the stylesheet to the header
*/
function add_css(){
echo '<link rel="stylesheet" href="'.get_bloginfo('wpurl').'/wp-content/plugins/tab-header-code/css/tabstyle.css" type="text/css" media="screen" />';
}
}
}
//instantiate the class
if (class_exists('tabstyles')) {
$tabstyles = new tabstyles();
}
?>
wp_enqueue_script is the right way of doing it. You could do the same thing Scott suggested, but the purpose of wp_enqueue_script is to avoid conflicts, which is something WP automatically handles.
Are you loading this on the front end or back end?
If back-end, I highly recommend limiting your JS files to only loading on your plugin pages, here’s how you do that:
For the front-end its more difficult to isolate your JS files to specific pages. The correct way to insert js files is similar, except I typically load them into the init, just as you did.
One more thing, be careful using __construct in your PHP code because it doesn’t work in PHP4, in case you plan on distributing your plugin, keep in mind some people are still using PHP4.
Good luck.
I think the correct method should be something like this:
?>
I’m more of a theme developer than plugin developer but I think that you can enqueue everything from just one callback.
Cheers
why not use
then have a insert_js function