Plugin Directory

Changeset 1319383


Ignore:
Timestamp:
12/31/2015 11:32:15 PM (10 years ago)
Author:
philip.newcomer
Message:

Version 1.1

Location:
widget-output-filters/trunk
Files:
5 added
2 edited

Legend:

Unmodified
Added
Removed
  • widget-output-filters/trunk/readme.txt

    r940170 r1319383  
    44Tags:              widget, widgets, filter, filters, output, html
    55Requires at least: 3.0
    6 Tested up to:      3.9.1
    7 Stable tag:        trunk
     6Tested up to:      4.4
     7Stable tag:        1.1
    88License:           GPLv2 or later
    9 License URI:       http://www.gnu.org/licenses/gpl-2.0.html
    109
    11 Enables developers to filter the output of WordPress widgets.
     10Enables developers to filter the output of any WordPress widget.
    1211
    1312== Description ==
    1413
    15 Sometimes developers need to filter the output of a widget that does not have its own output filter built-in. This plugin provides a filter which will allow developers to modify the complete HTML output of any widget.
     14Sometimes developers need to filter the output of a widget that does not have its own output filter built-in. This plugin provides a filter which will allow developers to filter any widget's output, regardless of whether it has that capability natively or not.
    1615
    17 This plugin is inspired by a similar filter in the [Widget Logic](https://wordpress.org/plugins/widget-logic/) plugin, and is useful in cases where a developer only needs to filter a widget's output, and does not require the widget visibility functionality of Widget Logic.
     16This plugin was inspired by a similar filter in the [Widget Logic](https://wordpress.org/plugins/widget-logic/) plugin, and essentially duplicates that functionality, but with more flexibility.
    1817
    19 This plugin also provides a filter parameter containing the widget type, or *base* ID (i.e. `meta` or `recent-posts`), in addition to the more specific widget ID parameter (`meta-2` or `recent-posts-5`). This allows developers to more easily filter all widgets of a specific type, rather than (as is the case with Widget Logic) only having the widget's own unique ID to work with, which will vary between individual widgets, widget areas, and WordPress installs.
     18Usage instructions are [on GitHub](https://github.com/philipnewcomer/widget-output-filters).
     19
     20This plugin is developed [on GitHub](https://github.com/philipnewcomer/widget-output-filters), and is available as a [Composer package](https://packagist.org/packages/philipnewcomer/wp-widget-output-filters).
    2021
    2122== Installation ==
    2223
    23 If you know what WordPress filters are, you probably know how to install a plugin. Just install and activate; there are no settings to configure. See the [usage instuctions](http://wordpress.org/plugins/widget-output-filters/other_notes/).
    24 
    25 == Other Notes ==
    26 
    27 This plugin provides a `widget_output` filter, with three parameters:
    28 
    29 1. The complete HTML output of the widget
    30 2. The widget type, or *base* ID; i.e. `meta` or `recent-posts`
    31 3. The complete widget ID; i.e. `meta-2` or `recent-posts-5`
    32 
    33 = Usage Example =
    34 
    35 `function my_widget_output_filter( $widget_output, $widget_type, $widget_id ) {
    36 
    37     if ( 'my_widget' == $widget_type ) {
    38         $widget_output = str_replace( 'something-to-find', 'something-to-replace', $widget_output );
    39     }
    40 
    41     return $widget_output;
    42 
    43 }
    44 add_filter( 'widget_output', 'my_widget_output_filter', 10, 3 );`
    45 
    46 == Frequently Asked Questions ==
    47 
    48 = Can't I just use output buffering in my sidebar.php template file, and modify widget output there? =
    49 
    50 Yes, but that method does not work in the WordPress Theme Customizer, or with widgets used in locations other than your output-buffered widget area. It also requires that widget area output buffering be built into the theme, and does not provide an easy way to target only one specific widget or widget type. This plugin provides widget type and widget ID parameters to your filter function, it works in the Theme Customizer, and does not require any support from the theme.
    51 
    52 = How is this plugin better than the widget_content filter already existing in the Widget Logic plugin? =
    53 
    54 This plugin provides the widget type (or *base* ID) in its filter, in addition to the unique widget ID. This makes it possible to target all widgets of a specific type, without resorting to complicated [regular expressions](http://en.wikipedia.org/wiki/Regular_expression). With the Widget Logic filter, a developer has access only to the individual widget ID in his filter function. Also, if you are using Widget Logic only for its `widget_content` filter, then this plugin will be lighter in weight than having the entire Widget Logic functionality active unnecessarily.
    55 
    56 = If I am already using the Widget Logic plugin to control the visibility of my widgets, is this plugin compatible with Widget Logic's widget_content filter? =
    57 
    58 Yes, this plugin coexists nicely with the Widget Logic plugin. However, if you are already using Widget Logic to manage the visibility of your widgets, the only reason you would need to use this plugin in addition to Widget Logic would be if you require the widget base ID to be available in your filter function, which Widget Logic does not provide. Otherwise Widget Logic's `widget_content` filter will do the job just as well.
     24If you know what WordPress filters are, you probably know how to install a plugin. Just install and activate; there are no settings to configure. See the [usage instructions on GitHub](https://github.com/philipnewcomer/widget-output-filters).
    5925
    6026== Changelog ==
    6127
     28= 1.1 =
     29* Refactor code
     30* Add Composer support
     31* Add sidebar ID as a parameter in the filter
    6232= 1.0 =
    6333* Initial release
  • widget-output-filters/trunk/widget-output-filters.php

    r940158 r1319383  
    11<?php
    2 /*
    3 Plugin Name: Widget Output Filters
    4 Plugin URI:  http://philipnewcomer.net/wordpress-plugins/widget-output-filters/
    5 Description: Enables developers to filter the output of WordPress widgets.
    6 Author:      Philip Newcomer
    7 Author URI:  http://philipnewcomer.net
    8 Version:     1.0
    9 License:     GPLv2 or later
    10 License URI: http://www.gnu.org/licenses/gpl-2.0.html
     2/**
     3 * Plugin Name: Widget Output Filters
     4 * Plugin URI:  http://philipnewcomer.net/wordpress-plugins/widget-output-filters/
     5 * Description: Enables developers to filter the output of any WordPress widget.
     6 * Author:      Philip Newcomer
     7 * Author URI:  http://philipnewcomer.net
     8 * Version:     1.1
     9 * License:     GPLv2 or later
     10 *
     11 * Copyright (C) 2015 Philip Newcomer
     12 * Based on code contained in the Widget Logic plugin by Alan Trewartha
     13 *
     14 * This program is free software; you can redistribute it and/or
     15 * modify it under the terms of the GNU General Public License
     16 * as published by the Free Software Foundation; either version 2
     17 * of the License, or (at your option) any later version.
     18 *
     19 * This program is distributed in the hope that it will be useful,
     20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     22 * GNU General Public License for more details.
     23 *
     24 * You should have received a copy of the GNU General Public License
     25 * along with this program; if not, write to the Free Software
     26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     27 */
    1128
    12 Copyright (C) 2014  Philip Newcomer
    13 Based on code contained in the Widget Logic plugin by Alan Trewartha
    14 
    15 This program is free software; you can redistribute it and/or
    16 modify it under the terms of the GNU General Public License
    17 as published by the Free Software Foundation; either version 2
    18 of the License, or (at your option) any later version.
    19 
    20 This program is distributed in the hope that it will be useful,
    21 but WITHOUT ANY WARRANTY; without even the implied warranty of
    22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    23 GNU General Public License for more details.
    24 
    25 You should have received a copy of the GNU General Public License
    26 along with this program; if not, write to the Free Software
    27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    28 */
    29 
    30 
    31 /**
    32  * Replaces the widget's display callback with the Dynamic Sidebar Params display callback, storing the original callback for use later.
    33  *
    34  * The $sidebar_params array is not modified; it is only used to get the current widget ID.
    35  *
    36  * @param  array $sidebar_params The sidebar parameters
    37  * @return array                 The sidebar parameters
    38  */
    39 function widget_output_filters_dynamic_sidebar_params( $sidebar_params ) {
    40 
    41     if ( is_admin() ) {
    42         return $sidebar_params;
    43     }
    44 
    45     global $wp_registered_widgets;
    46     $widget_id = $sidebar_params[0]['widget_id'];
    47 
    48     $wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
    49     $wp_registered_widgets[ $widget_id ]['callback'] = 'widget_output_filters_display_widget';
    50 
    51     return $sidebar_params;
    52 
    53 }
    54 add_filter( 'dynamic_sidebar_params', 'widget_output_filters_dynamic_sidebar_params', 9 ); // Priority of 9 to run before the Widget Logic plugin
    55 
    56 
    57 /**
    58  * Callback function to display the widget's original callback function output, with filtering.
    59  */
    60 function widget_output_filters_display_widget() {
    61 
    62     global $wp_registered_widgets;
    63     $original_callback_params = func_get_args();
    64     $widget_id = $original_callback_params[0]['widget_id'];
    65 
    66     $original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
    67     $wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;
    68 
    69     $widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base;
    70 
    71     if ( is_callable( $original_callback ) ) {
    72 
    73         ob_start();
    74         call_user_func_array( $original_callback, $original_callback_params );
    75         $widget_output = ob_get_clean();
    76 
    77         echo apply_filters( 'widget_output', $widget_output, $widget_id_base, $widget_id );
    78 
    79     }
    80 
    81 }
     29require_once( __DIR__ . '/src/class.Widget_Output_Filters.php' );
     30new Widget_Output_Filters();
Note: See TracChangeset for help on using the changeset viewer.