Chances are, you’re running a WPMU in some sort of niche. On top of that, you probably have some users who want to place particular javascript in a text widget. By default, this gets stripped out. What to do?
Why, build a widget for them, of course!
Here’s an incredibly simple BASIC widget that does nothing but spew out whatever code you paste in there. There’s no title box, no inputs from users, just a space to paste code.
/*
Plugin Name: Build A Widget
Description: Adds a sidebar widget to display any embedded content
Author: Andrea_R
based on Automattic code
Version: 1.0
Author URI: http://ronandandrea.com
*/
function widget_buildablank_init() {
if ( !function_exists(‘register_sidebar_widget’) )
return;
function widget_buildablank($args) {
extract($args);
// You can change the title below to whatever you like. This shows on the Widgets page
$title = ‘Build a Widget’;
// These lines generate our output.
echo $before_widget . $before_title . $title . $after_title;
echo ‘Whatever code we paste in here will be run in our widget. Replace this text with your ad code, offsite javascript, whatever.’;
echo $after_widget;
}
// Replace the description below with your own
register_sidebar_widget(‘Your New Widget Description Here’, ‘widget_buildablank’);
}
// Run our code later in case this loads prior to any required plugins.
add_action(‘plugins_loaded’, ‘widget_buildablank_init’);
?>
Blank Build-A-Widget (1176)
(download file is now .txt, please save and rename to .php)
The commented areas are pretty self-explanatory. Just remember to substitute your function names for the generic ones, and paste in your code right where it say between the single quotes. Upload it to the plugins folder for user-choice, or even the mu-plugins folder. Since this is a widget, users who can access the widget area can add or remove it to their sidebar.
You could use this widget for javascript from many advertising places, for a site badge, for your custom code or even existing plugin code that you want widgetized – a lot of possibilities. Or you can build off it, add things, and get more complicated (If you can’t, don’t worry, I’ll have another one upcoming with more options.
)


Great news. Thanks.
I can’t wait for the next one too.
Awesome. I’m setting up my first MU site and was just thinking it would be neat to be able create a widget to place across all the blogs!
Nice work, looking forward to the next one.
Thanks for the tutorial Andrea, It’s a great post.
Hi Andrea
Thanks for the post. I am trying to make widget that embeds the openx code. But it give me errors.
I place the following openx hosted code in the correct location!
I tried using the image tag and javascript but to no avail!
Any ideas?
Excellent thanks! I’ll definitely be trying out the tutorial.
Hey,
Can I also use php code in this widget?
Thanks.
you’re better off including your own php code within the widget code itself.
That makes sense
thanks
Andrea,
I am trying to use your plugin to run the following js code:
if(SUTRA_WDG_ID) SUTRA_WDG_ID++; else var SUTRA_WDG_ID = 1;
if(!LIBRARY_LOADED) var LIBRARY_LOADED = false;
this["sutraWdgObj" + this["SUTRA_WDG_ID"]] = new SutraJobWidget();
this["sutraWdgObj" + this["SUTRA_WDG_ID"]].WIDGET_ID = SUTRA_WDG_ID;
this["sutraWdgObj" + this["SUTRA_WDG_ID"]].COMPANY_ID_LIST = ["1", "4", "64", "118"];
this["sutraWdgObj" + this["SUTRA_WDG_ID"]].APP_ID = “5ac951d6d4e75df90cbe2fe9b463dc4c”;
this["sutraWdgObj" + this["SUTRA_WDG_ID"]].ROLE = “b”;
this["sutraWdgObj" + this["SUTRA_WDG_ID"]].LoadLibrary();
(not sure if your blog will strip out the code..)
However, it does allow me to activate it because of the following error:
Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’ in /home/kunal17/public_html/theindianstartup/wp-content/plugins/jswidget.php on line 22
Can you help?
Hi,
Useful tutorial, also i wonder, can i make default custom “text” widget with use of default widget?
Thanks.