-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap-widget-filters.php
More file actions
95 lines (83 loc) · 4.62 KB
/
bootstrap-widget-filters.php
File metadata and controls
95 lines (83 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* Wordpress Widget Filters
*
* @package Wordpress Widget Filters
* @author Bryan Willis
* @license GPL-2.0+
* @link http://wordpress.stackexchange.com/a/211634/43806
*
* @wordpress-plugin
* Plugin Name: Wordpress Widget Filters
* Plugin URI: https://github.com/Wordpress-Development/bootstrap-widgets.php
* Description: Add Bootstrap to wordpress widgets. Widget Output Filters plugin included. Requires Widget Output Filters Plugin and theme with Bootstrap 3 support.
* Version: 1.0.0
* Author: Bryan Willis
* Author URI: http://profiles.wordpress.org/codecandid
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
/**
* This plugin does the following on activation:
* - Checks if Widget Output Filters is activated and if not deactivate itself.
* - Filters the html markup to support Bootstrap 3 styling. Without Botstrap enqueued it won't appear to do anything.
*/
defined( 'WPINC' ) or die;
register_activation_hook( __FILE__, 'activate_wop_bootstrap_register_activation_hook' );
function activate_wop_bootstrap_register_activation_hook() {
$admin_url = admin_url().'/plugin-install.php?tab=plugin-information&plugin=widget-output-filters';
if ( ! function_exists('widget_output_filters_dynamic_sidebar_params') ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( sprintf( __( 'This theme requires a bootstrap supported theme and Widget Output Filters to work. You can\'t activate %1$sBootstrap-Widget-Filters%2$s until you install or activate the %3$sWidget Output Filters Plugin%4$s. Go back to the %5$sPlugins Page%4$s.' ), '<em>', '</em>', '<a href="'.$admin_url.'" target="_blank">', '</a>', '<a href="javascript:history.back()">' ) );
}
}
/**
* Bootstrap 3 support for core wordpress widgets
*/
function brw_bootstrap_widget_output_filters( $widget_output, $widget_type, $widget_id ) {
switch( $widget_type ) {
case 'categories' :
$widget_output = str_replace('<ul>', '<ul class="list-group">', $widget_output);
$widget_output = str_replace('<li class="cat-item cat-item-', '<li class="list-group-item cat-item cat-item-', $widget_output);
$widget_output = str_replace('(', '<span class="badge cat-item-count"> ', $widget_output);
$widget_output = str_replace(')', ' </span>', $widget_output);
break;
case 'calendar' :
$widget_output = str_replace('calendar_wrap', 'calendar_wrap table-responsive', $widget_output);
$widget_output = str_replace('<table id="wp-calendar', '<table class="table table-condensed" id="wp-calendar', $widget_output);
break;
case 'tag_cloud' :
$regex = "/(<a[^>]+?)( style='font-size:.+pt;'>)([^<]+?)(<\/a>)/"; // can probably be cleaned up
$replace_with = "$1><span class='label label-primary'>$3</span>$4";
$widget_output = preg_replace( $regex , $replace_with , $widget_output );
break;
case 'archives' :
$widget_output = str_replace('<ul>', '<ul class="list-group">', $widget_output);
$widget_output = str_replace('<li>', '<li class="list-group-item archive-list-group-item">', $widget_output);
$widget_output = str_replace('(', '<span class="badge cat-item-count"> ', $widget_output);
$widget_output = str_replace(')', ' </span>', $widget_output);
break;
case 'meta' :
$widget_output = str_replace('<ul>', '<ul class="list-group">', $widget_output);
$widget_output = str_replace('<li>', '<li class="list-group-item meta-list-group-item">', $widget_output);
break;
case 'recent-posts' :
$widget_output = str_replace('<ul>', '<ul class="list-group">', $widget_output);
$widget_output = str_replace('<li>', '<li class="list-group-item recent-posts-list-group-item">', $widget_output);
break;
case 'recent-comments' :
$widget_output = str_replace('<ul id="recentcomments">', '<ul id="recentcomments" class="list-group">', $widget_output);
$widget_output = str_replace('<li class="recentcomments">', '<li class="recentcomments list-group-item recent-comments-list-group-item">', $widget_output);
break;
case 'pages' :
$widget_output = str_replace('<ul>', '<ul class="nav nav-stacked nav-pills">', $widget_output);
break;
case 'nav_menu' :
$widget_output = str_replace(' class="menu"', 'class="menu nav nav-stacked nav-pills"', $widget_output);
break;
default:
$widget_output = $widget_output; // not sure if this is needed...
}
return $widget_output;
}
add_filter( 'widget_output', 'brw_bootstrap_widget_output_filters', 10, 3 );