1 ) { $_args['name'] = isset($args['name']) ? $args['name'] : sprintf(__('Sidebar %d'), $i); } else { $_args['name'] = isset($args['name']) ? $args['name'] : __('Sidebar'); } $_args['id'] = isset($args['id']) ? $args['id'] : "sidebar-$i"; register_sidebar($_args); } } function register_sidebar($args = array()) { global $wp_registered_sidebars; if ( is_string($args) ) parse_str($args, $args); $i = count($wp_registered_sidebars) + 1; $defaults = array( 'name' => sprintf(__('Sidebar %d'), $i ), 'id' => "sidebar-$i", 'before_widget' => '
  • ', 'after_widget' => "
  • \n", 'before_title' => '

    ', 'after_title' => "

    \n", ); $sidebar = array_merge($defaults, $args); $wp_registered_sidebars[$sidebar['id']] = $sidebar; return $sidebar['id']; } function unregister_sidebar( $name ) { global $wp_registered_sidebars; if ( isset( $wp_registered_sidebars[$name] ) ) unset( $wp_registered_sidebars[$name] ); } function register_sidebar_widget($name, $output_callback, $classname = '') { // Compat if ( is_array($name) ) { if ( count($name) == 3 ) $name = sprintf($name[0], $name[2]); else $name = $name[0]; } $id = sanitize_title($name); $options = array(); if ( !empty($classname) && is_string($classname) ) $options['classname'] = $classname; $params = array_slice(func_get_args(), 2); $args = array($id, $name, $output_callback, $options); if ( !empty($params) ) $args = array_merge($args, $params); call_user_func_array('wp_register_sidebar_widget', $args); } function wp_register_sidebar_widget($id, $name, $output_callback, $options = array()) { global $wp_registered_widgets, $wp_register_widget_defaults; if ( empty($output_callback) ) { unset($wp_registered_widgets[$id]); return; } $defaults = array('classname' => $output_callback); $options = wp_parse_args($options, $defaults); $widget = array( 'name' => $name, 'id' => $id, 'callback' => $output_callback, 'params' => array_slice(func_get_args(), 4) ); $widget = array_merge($widget, $options); if ( is_callable($output_callback) && ( !isset($wp_registered_widgets[$id]) || !$wp_register_widget_defaults) ) $wp_registered_widgets[$id] = $widget; } function unregister_sidebar_widget($id) { return wp_unregister_sidebar_widget($id); } function wp_unregister_sidebar_widget($id) { wp_register_sidebar_widget($id, '', ''); wp_unregister_widget_control($id); } function register_widget_control($name, $control_callback, $width = '', $height = '') { // Compat if ( is_array($name) ) { if ( count($name) == 3 ) $name = sprintf($name[0], $name[2]); else $name = $name[0]; } $id = sanitize_title($name); $options = array(); if ( !empty($width) ) $options['width'] = $width; if ( !empty($height) ) $options['height'] = $height; $params = array_slice(func_get_args(), 4); $args = array($id, $name, $control_callback, $options); if ( !empty($params) ) $args = array_merge($args, $params); call_user_func_array('wp_register_widget_control', $args); } function wp_register_widget_control($id, $name, $control_callback, $options = array()) { global $wp_registered_widget_controls, $wp_register_widget_defaults; if ( empty($control_callback) ) { unset($wp_registered_widget_controls[$id]); return; } if ( isset($wp_registered_widget_controls[$id]) && $wp_register_widget_defaults ) return; $defaults = array('width' => 300, 'height' => 200); $options = wp_parse_args($options, $defaults); $options['width'] = (int) $options['width']; $options['height'] = (int) $options['height']; $options['width'] = $options['width'] > 90 ? $options['width'] + 60 : 360; $options['height'] = $options['height'] > 60 ? $options['height'] + 40 : 240; $widget = array( 'name' => $name, 'id' => $id, 'callback' => $control_callback, 'params' => array_slice(func_get_args(), 4) ); $widget = array_merge($widget, $options); $wp_registered_widget_controls[$id] = $widget; } function unregister_widget_control($id) { return wp_unregister_widget_control($id); } function wp_unregister_widget_control($id) { return wp_register_widget_control($id, '', ''); } function dynamic_sidebar($index = 1) { global $wp_registered_sidebars, $wp_registered_widgets; if ( is_int($index) ) { $index = "sidebar-$index"; } else { $index = sanitize_title($index); foreach ( $wp_registered_sidebars as $key => $value ) { if ( sanitize_title($value['name']) == $index ) { $index = $key; break; } } } $sidebars_widgets = wp_get_sidebars_widgets(); if ( empty($wp_registered_sidebars[$index]) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) ) return false; $sidebar = $wp_registered_sidebars[$index]; $did_one = false; foreach ( $sidebars_widgets[$index] as $id ) { $callback = $wp_registered_widgets[$id]['callback']; $params = array_merge(array($sidebar), (array) $wp_registered_widgets[$id]['params']); // Substitute HTML id and class attributes into before_widget $classname_ = ''; foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) { if ( is_string($cn) ) $classname_ .= '_' . $cn; elseif ( is_object($cn) ) $classname_ .= '_' . get_class($cn); } $classname_ = ltrim($classname_, '_'); $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_); if ( is_callable($callback) ) { call_user_func_array($callback, $params); $did_one = true; } } return $did_one; } function is_active_widget($callback) { global $wp_registered_widgets; $sidebars_widgets = wp_get_sidebars_widgets(false); if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets ) if ( is_array($widgets) ) foreach ( $widgets as $widget ) if ( $wp_registered_widgets[$widget]['callback'] == $callback ) return true; return false; } function is_dynamic_sidebar() { global $wp_registered_widgets, $wp_registered_sidebars; $sidebars_widgets = get_option('sidebars_widgets'); foreach ( $wp_registered_sidebars as $index => $sidebar ) { if ( count($sidebars_widgets[$index]) ) { foreach ( $sidebars_widgets[$index] as $widget ) if ( array_key_exists($widget, $wp_registered_widgets) ) return true; } } return false; } /* Internal Functions */ function wp_get_sidebars_widgets($update = true) { global $wp_registered_widgets, $wp_registered_sidebars; $sidebars_widgets = get_option('sidebars_widgets'); $_sidebars_widgets = array(); if ( !isset($sidebars_widgets['array_version']) ) $sidebars_widgets['array_version'] = 1; switch ( $sidebars_widgets['array_version'] ) { case 1 : foreach ( $sidebars_widgets as $index => $sidebar ) if ( is_array($sidebar) ) foreach ( $sidebar as $i => $name ) { $id = strtolower($name); if ( isset($wp_registered_widgets[$id]) ) { $_sidebars_widgets[$index][$i] = $id; continue; } $id = sanitize_title($name); if ( isset($wp_registered_widgets[$id]) ) { $_sidebars_widgets[$index][$i] = $id; continue; } unset($_sidebars_widgets[$index][$i]); } $_sidebars_widgets['array_version'] = 2; $sidebars_widgets = $_sidebars_widgets; unset($_sidebars_widgets); case 2 : $sidebars = array_keys( $wp_registered_sidebars ); if ( !empty( $sidebars ) ) { // Move the known-good ones first foreach ( $sidebars as $id ) { if ( array_key_exists( $id, $sidebars_widgets ) ) { $_sidebars_widgets[$id] = $sidebars_widgets[$id]; unset($sidebars_widgets[$id], $sidebars[$id]); } } // Assign to each unmatched registered sidebar the first available orphan unset( $sidebars_widgets[ 'array_version' ] ); while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) ) $_sidebars_widgets[ $sidebar ] = $widgets; $_sidebars_widgets['array_version'] = 3; $sidebars_widgets = $_sidebars_widgets; unset($_sidebars_widgets); } if ( $update ) update_option('sidebars_widgets', $sidebars_widgets); } unset($sidebars_widgets['array_version']); return $sidebars_widgets; } function wp_set_sidebars_widgets( $sidebars_widgets ) { update_option( 'sidebars_widgets', $sidebars_widgets ); } function wp_get_widget_defaults() { global $wp_registered_sidebars; $defaults = array(); foreach ( $wp_registered_sidebars as $index => $sidebar ) $defaults[$index] = array(); return $defaults; } /* Default Widgets */ function wp_widget_pages( $args ) { extract( $args ); $options = get_option( 'widget_pages' ); $title = empty( $options['title'] ) ? __( 'Pages' ) : $options['title']; $sortby = empty( $options['sortby'] ) ? 'menu_order' : $options['sortby']; $exclude = empty( $options['exclude'] ) ? '' : $options['exclude']; if ( $sortby == 'menu_order' ) { $sortby = 'menu_order, post_title'; } $out = wp_list_pages( array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) ); if ( !empty( $out ) ) { ?>


    $before_title, 'title_after' => $after_title, 'category_before' => $before_widget, 'category_after' => $after_widget, 'show_images' => true, 'class' => 'linkcat widget' )); } function wp_widget_search($args) { extract($args); ?>

    '; get_calendar(); echo ''; echo $after_widget; } function wp_widget_calendar_control() { $options = $newoptions = get_option('widget_calendar'); if ( $_POST["calendar-submit"] ) { $newoptions['title'] = strip_tags(stripslashes($_POST["calendar-title"])); } if ( $options != $newoptions ) { $options = $newoptions; update_option('widget_calendar', $options); } $title = attribute_escape($options['title']); ?>

    " name="text-submit-" value="1" /> 9 ) $number = 9; if ( $number < 1 ) $number = 1; $newoptions['number'] = $number; } if ( $options != $newoptions ) { $options = $newoptions; update_option('widget_text', $options); wp_widget_text_register($options['number']); } } function wp_widget_text_page() { $options = $newoptions = get_option('widget_text'); ?>

    9 ) $number = 9; $dims = array('width' => 460, 'height' => 350); $class = array('classname' => 'widget_text'); for ($i = 1; $i <= 9; $i++) { $name = sprintf(__('Text %d'), $i); $id = "text-$i"; // Never never never translate an id wp_register_sidebar_widget($id, $name, $i <= $number ? 'wp_widget_text' : /* unregister */ '', $class, $i); wp_register_widget_control($id, $name, $i <= $number ? 'wp_widget_text_control' : /* unregister */ '', $dims, $i); } add_action('sidebar_admin_setup', 'wp_widget_text_setup'); add_action('sidebar_admin_page', 'wp_widget_text_page'); } function wp_widget_categories($args, $number = 1) { extract($args); $options = get_option('widget_categories'); $c = $options[$number]['count'] ? '1' : '0'; $h = $options[$number]['hierarchical'] ? '1' : '0'; $d = $options[$number]['dropdown'] ? '1' : '0'; $title = empty($options[$number]['title']) ? __('Categories') : $options[$number]['title']; echo $before_widget; echo $before_title . $title . $after_title; $cat_args = "orderby=name&show_count={$c}&hierarchical={$h}"; if ( $d ) { wp_dropdown_categories($cat_args . '&show_option_none= ' . __('Select Category')); ?>

    9 ) { $number = 9; } elseif ( $number < 1 ) { $number = 1; } $newoptions['number'] = $number; } if ( $newoptions != $options ) { $options = $newoptions; update_option( 'widget_categories', $options ); wp_widget_categories_register( $options['number'] ); } } function wp_widget_categories_page() { $options = get_option( 'widget_categories' ); ?>

    1, 1 => $options ); update_option( 'widget_categories', $newoptions ); $sidebars_widgets = get_option( 'sidebars_widgets' ); if ( is_array( $sidebars_widgets ) ) { foreach ( $sidebars_widgets as $sidebar => $widgets ) { if ( is_array( $widgets ) ) { foreach ( $widgets as $widget ) $new_widgets[$sidebar][] = ( $widget == 'categories' ) ? 'categories-1' : $widget; } else { $new_widgets[$sidebar] = $widgets; } } if ( $new_widgets != $sidebars_widgets ) update_option( 'sidebars_widgets', $new_widgets ); } if ( isset( $_POST['categories-submit'] ) ) { $_POST['categories-submit-1'] = $_POST['categories-submit']; $_POST['categories-count-1'] = $_POST['categories-count']; $_POST['categories-hierarchical-1'] = $_POST['categories-hierarchical']; $_POST['categories-dropdown-1'] = $_POST['categories-dropdown']; $_POST['categories-title-1'] = $_POST['categories-title']; foreach ( $_POST as $k => $v ) if ( substr($k, -5) == 'order' ) $_POST[$k] = str_replace('categories', 'categories-1', $v); } return $newoptions; } function wp_widget_categories_register() { $options = get_option( 'widget_categories' ); if ( !isset($options['number']) ) $options = wp_widget_categories_upgrade(); $number = (int) $options['number']; if ( $number > 9 ) { $number = 9; } elseif ( $number < 1 ) { $number = 1; } $dims = array( 'width' => 350, 'height' => 170 ); $class = array( 'classname' => 'widget_categories' ); for ( $i = 1; $i <= 9; $i++ ) { $name = sprintf( __( 'Categories %d' ), $i ); $id = 'categories-' . $i; $widget_callback = ( $i <= $number ) ? 'wp_widget_categories' : ''; $control_callback = ( $i <= $number ) ? 'wp_widget_categories_control' : ''; wp_register_sidebar_widget( $id, $name, $widget_callback, $class, $i ); wp_register_widget_control( $id, $name, $control_callback, $dims, $i ); } add_action( 'sidebar_admin_setup', 'wp_widget_categories_setup' ); add_action( 'sidebar_admin_page', 'wp_widget_categories_page' ); } function wp_widget_recent_entries($args) { if ( $output = wp_cache_get('widget_recent_entries') ) return print($output); ob_start(); extract($args); $options = get_option('widget_recent_entries'); $title = empty($options['title']) ? __('Recent Posts') : $options['title']; if ( !$number = (int) $options['number'] ) $number = 10; else if ( $number < 1 ) $number = 1; else if ( $number > 15 ) $number = 15; $r = new WP_Query("showposts=$number&what_to_show=posts&nopaging=0&post_status=publish"); if ($r->have_posts()) : ?>

    15 ) $number = 15; if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) { $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number"); wp_cache_add( 'recent_comments', $comments, 'widget' ); } ?>

    320, 'height' => 90); $class = array('classname' => 'widget_recent_comments'); wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $class); wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control', $dims); if ( is_active_widget('wp_widget_recent_comments') ) add_action('wp_head', 'wp_widget_recent_comments_style'); } function wp_widget_rss($args, $number = 1) { require_once(ABSPATH . WPINC . '/rss.php'); extract($args); $options = get_option('widget_rss'); if ( isset($options['error']) && $options['error'] ) return; $num_items = (int) $options[$number]['items']; $show_summary = $options[$number]['show_summary']; if ( empty($num_items) || $num_items < 1 || $num_items > 10 ) $num_items = 10; $url = $options[$number]['url']; while ( strstr($url, 'http') != $url ) $url = substr($url, 1); if ( empty($url) ) return; $rss = fetch_rss($url); $link = clean_url(strip_tags($rss->channel['link'])); while ( strstr($link, 'http') != $link ) $link = substr($link, 1); $desc = attribute_escape(strip_tags(html_entity_decode($rss->channel['description'], ENT_QUOTES))); $title = $options[$number]['title']; if ( empty($title) ) $title = htmlentities(strip_tags($rss->channel['title'])); if ( empty($title) ) $title = $desc; if ( empty($title) ) $title = __('Unknown Feed'); $url = clean_url(strip_tags($url)); if ( file_exists(dirname(__FILE__) . '/rss.png') ) $icon = str_replace(ABSPATH, get_option('siteurl').'/', dirname(__FILE__)) . '/rss.png'; else $icon = get_option('siteurl').'/wp-includes/images/rss.png'; $title = "RSS $title"; ?> items ) && !empty( $rss->items ) ) { $rss->items = array_slice($rss->items, 0, $num_items); echo ''; } else { echo ''; } echo $after_widget; } function wp_widget_rss_control($number) { $options = $newoptions = get_option('widget_rss'); if ( $_POST["rss-submit-$number"] ) { $newoptions[$number]['items'] = (int) $_POST["rss-items-$number"]; $url = sanitize_url(strip_tags(stripslashes($_POST["rss-url-$number"]))); $newoptions[$number]['title'] = trim(strip_tags(stripslashes($_POST["rss-title-$number"]))); if ( $url !== $options[$number]['url'] ) { require_once(ABSPATH . WPINC . '/rss.php'); $rss = fetch_rss($url); if ( is_object($rss) ) { $newoptions[$number]['url'] = $url; $newoptions[$number]['error'] = false; } else { $newoptions[$number]['error'] = true; $newoptions[$number]['url'] = wp_specialchars(__('Error: could not find an RSS or ATOM feed at that URL.'), 1); $error = sprintf(__('Error in RSS %1$d: %2$s'), $number, $newoptions[$number]['error']); } } } if ( $options != $newoptions ) { $options = $newoptions; update_option('widget_rss', $options); } $url = attribute_escape($options[$number]['url']); $items = (int) $options[$number]['items']; $title = attribute_escape($options[$number]['title']); if ( empty($items) || $items < 1 ) $items = 10; ?>

    " name="rss-url-" type="text" value="" />

    " name="rss-title-" type="text" value="" />

    " name="rss-submit-" value="1" /> 9 ) $number = 9; if ( $number < 1 ) $number = 1; $newoptions['number'] = $number; } if ( $options != $newoptions ) { $options = $newoptions; update_option('widget_rss', $options); wp_widget_rss_register($options['number']); } } function wp_widget_rss_page() { $options = $newoptions = get_option('widget_rss'); ?>

    9 ) $number = 9; $dims = array('width' => 410, 'height' => 200); $class = array('classname' => 'widget_rss'); for ($i = 1; $i <= 9; $i++) { $name = sprintf(__('RSS %d'), $i); $id = "rss-$i"; // Never never never translate an id wp_register_sidebar_widget($id, $name, $i <= $number ? 'wp_widget_rss' : /* unregister */ '', $class, $i); wp_register_widget_control($id, $name, $i <= $number ? 'wp_widget_rss_control' : /* unregister */ '', $dims, $i); } add_action('sidebar_admin_setup', 'wp_widget_rss_setup'); add_action('sidebar_admin_page', 'wp_widget_rss_page'); } function wp_widget_tag_cloud($args) { extract($args); $options = get_option('widget_tag_cloud'); $title = empty($options['title']) ? __('Tags') : $options['title']; echo $before_widget; echo $before_title . $title . $after_title; wp_tag_cloud(); echo $after_widget; } function wp_widget_tag_cloud_control() { $options = $newoptions = get_option('widget_tag_cloud'); if ( $_POST['tag-cloud-submit'] ) { $newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title'])); } if ( $options != $newoptions ) { $options = $newoptions; update_option('widget_tag_cloud', $options); } $title = attribute_escape( $options['title'] ); ?>

    90, 'width' => 300 ); $dims100 = array( 'height' => 100, 'width' => 300 ); $dims150 = array( 'height' => 150, 'width' => 300 ); $class = array('classname' => 'widget_pages'); wp_register_sidebar_widget('pages', __('Pages'), 'wp_widget_pages', $class); wp_register_widget_control('pages', __('Pages'), 'wp_widget_pages_control', $dims150); $class['classname'] = 'widget_calendar'; wp_register_sidebar_widget('calendar', __('Calendar'), 'wp_widget_calendar', $class); wp_register_widget_control('calendar', __('Calendar'), 'wp_widget_calendar_control', $dims90); $class['classname'] = 'widget_archives'; wp_register_sidebar_widget('archives', __('Archives'), 'wp_widget_archives', $class); wp_register_widget_control('archives', __('Archives'), 'wp_widget_archives_control', $dims100); $class['classname'] = 'widget_links'; wp_register_sidebar_widget('links', __('Links'), 'wp_widget_links', $class); $class['classname'] = 'widget_meta'; wp_register_sidebar_widget('meta', __('Meta'), 'wp_widget_meta', $class); wp_register_widget_control('meta', __('Meta'), 'wp_widget_meta_control', $dims90); $class['classname'] = 'widget_search'; wp_register_sidebar_widget('search', __('Search'), 'wp_widget_search', $class); $class['classname'] = 'widget_recent_entries'; wp_register_sidebar_widget('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries', $class); wp_register_widget_control('recent-posts', __('Recent Posts'), 'wp_widget_recent_entries_control', $dims90); $class['classname'] = 'widget_tag_cloud'; wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $class); wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control', 'width=300&height=160'); wp_widget_categories_register(); wp_widget_text_register(); wp_widget_rss_register(); wp_widget_recent_comments_register(); $GLOBALS['wp_register_widget_defaults'] = false; do_action('widgets_init'); } add_action('init', 'wp_widgets_init', 1); ?>