Wordpress Widgets - not just for the sidebar.

The files below are from my hacked version of widgets. It implements a render replacement of [[Widget Name]] with a call to that widget. This means widgets are freed from the sidebar and can be embedded into a posting.

  • widgets/README.txt - needs updating
  • widgets/widgets.php - see diff
  • widgets/wpmu_widget_bindings.php - this maps [[]] names to functions
  • widgets/mrjc_widgetize_function.php - this holds the bulk of my mods (see below).

widgets/Widgets.php diff:

[crack]$ rcsdiff -r1 widgets.php

===================================================================
RCS file: widgets.php,v
retrieving revision 1.1
diff -r1.1 widgets.php
75a76,77
> //    echo "registering '$name' for '$output_callback', '$classname'<P>";
>
86c88,91
<
---
>
>
>       $params = array_slice(func_get_args(), 2);
>
91c96
<               'params' => array_slice(func_get_args(), 2)
---
>               'params' => $params
94c99,100
<       if ( empty($output_callback) )
---
>
>       if ( empty($output_callback) ) {
95a102
>       }
164a172
>
165a174
> //    echo "<pre>";var_dump($params); echo "</pre><p/>";
168a178,179
>       } else {
>               echo "No callback registered for widget named '$name'";
1192,1194c1203,1207
<       $temp = explode('[[widget]]',$content);
<       if(sizeof($temp)>1) {
<               $widget_name = "List All Posts";
---
> // Ideally this would find all occurances, and preserve the text between them.
>       $widget_call_start = strstr($content, '[[');
>       $widget_call_end = strstr($content, ']]');
>       if ($widget_call_start && $widget_call_end) {
>               $widget_name = substr($content, $widget_call_start+2, $widget_call_end-2);
1196c1209
<                       return $content.'Missing parameter, $widget_name';
---
>                       return $content.'Missing parameter, $widget_name ($widget_call_start, $widget_call_end';
1197a1211
> //                    echo $widget_name;
 wpmu_widget_bindings.php

<?php
// Note this has to be evaluated after widgets.php & therefore must be named wj* or later in the
// directory

register_sidebar_widget(
“Today’s Posts”, ‘mrjc_widgetize_function’,”, ‘ah_recent_posts_mu’, “Today’s Posts”, ‘100′, ‘name’, ‘<li>’
, ‘</li>’, ‘updated’
);

register_sidebar_widget(
‘List All Blogs’, ‘mrjc_widgetize_function’, ”, ‘list_all_wpmu_blogs’, ‘All Blogs’, ‘100′, ‘name’, ‘<li>’,
‘</li>’, ‘updated’
);

register_sidebar_widget(
‘List All Posts’, ‘mrjc_widgetize_function’, ”, ‘list_all_wpmu_posts’, ‘All Posts’, 30, 100, ‘<i>’, ‘</i>’
, ‘<LI>’, ‘… ‘,’show’,’show’,’show’

);

register_sidebar_widget(
‘List Newest Blogs’, ‘mrjc_widgetize_function’, ”, fimii_wpmu_newest_blogs, “Newest Blogs”, 5
);

register_sidebar_widget(
‘Recent Comments’, ‘mrjc_widgetize_function’, ”, mdv_recent_comments, “Recent Comments”, 5
);

?>
[crack]$ more mrjc_widgetize_function.php
<?php
function mrjc_widgetize_function($args, $class, $function, $title) {
$params = array_slice(func_get_args(), 4);
//      print “Widgetized $function(<p>”;
//      print “<pre>”;var_dump($params);print “</pre>”;
//      print “)<p>”;
extract($args); // defines before_widget, before_title, after_title
if(function_exists($function)) {
echo $before_widget;

echo $before_title . $title . $after_title;
echo “<ul>n”;

call_user_func_array($function, $params);
echo “</ul>n”;
echo $after_widget;
} else {
echo “<p>Error - function ‘$function’ not found</p>”;
}
}

?>
I’ve mailed the wp team to ask if it can go in the distro.

Related Posts

Leave a Reply