diff --git a/README.md b/README.md new file mode 100644 index 0000000..810e19c --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# Widget Subtitles # +Add a customizable subtitle to your widgets + +[![WordPress Plugin version](https://img.shields.io/wordpress/plugin/v/widget-subtitles.svg?style=flat)](https://wordpress.org/plugins/widget-subtitles/) +[![WordPress Plugin WP tested version](https://img.shields.io/wordpress/v/widget-subtitles.svg?style=flat)](https://wordpress.org/plugins/widget-subtitles/) +[![WordPress Plugin downloads](https://img.shields.io/wordpress/plugin/dt/widget-subtitles.svg?style=flat)](https://wordpress.org/plugins/widget-subtitles/) +[![WordPress Plugin rating](https://img.shields.io/wordpress/plugin/r/widget-subtitles.svg?style=flat)](https://wordpress.org/plugins/widget-subtitles/) +[![Travis](https://secure.travis-ci.org/JoryHogeveen/widget-subtitles.png?branch=master)](http://travis-ci.org/JoryHogeveen/widget-subtitles) +[![License](https://img.shields.io/badge/license-GPL--2.0%2B-green.svg)](https://github.com/JoryHogeveen/widget-subtitles/blob/master/license.txt) +[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=YGPLMLU7XQ9E8&lc=NL&item_name=Widget%20Subtitles&item_number=JWPP%2dWS¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest) + +## Description + +This plugin adds a subtitle input field to all your widgets. You can also change the location of the subtitle and even use filters to change the subtitle output. + +### Filters + +#### `widget_subtitles_element` +Allows you to change the HTML element for the subtitle + +* **Default:** span +* **Parameters:** + * string (default) + * string (widget id) + * string (sidebar id) + * array (widget data) + +#### `widget_subtitles_classes` +Allows you to change the HTML element for the subtitle + +* **Default:** `array( 'widget-subtitle', 'widgetsubtitle', 'subtitle-LOCATION' );` Where LOCATION stands for your selected location +* **Parameters:** + * array (default classes) + * string (widget id) + * string (sidebar id) + * array (widget data) + +## Installation + +1. Upload `/widget-subtitles` to the `/wp-content/plugins/` directory +2. Activate the plugin through the *Plugins* menu in WordPress +3. Go to *Appearance* > *Widgets* menu and fill out your subtitles diff --git a/readme.txt b/readme.txt index 3759479..06a9fb1 100644 --- a/readme.txt +++ b/readme.txt @@ -3,8 +3,8 @@ Contributors: keraweb Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=YGPLMLU7XQ9E8&lc=NL&item_name=Widget%20Subtitles&item_number=JWPP%2dWS¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest Tags: widget, widget subtitle, subtitle, subtitles, sub title, sidebar Requires at least: 3.0 -Tested up to: 4.6 -Stable tag: 1.0.1 +Tested up to: 4.7 +Stable tag: 1.1 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -18,13 +18,21 @@ This plugin adds a subtitle input field to all your widgets. You can also change Allows you to change the HTML element for the subtitle * **Default:** span -* **Parameters:** string (default), int (widget id) +* **Parameters:** + * string (default) + * string (widget id) + * string (sidebar id) + * array (widget data) = Filter: `widget_subtitles_classes` = Allows you to change the HTML element for the subtitle * **Default:** `array( 'widget-subtitle', 'widgetsubtitle', 'subtitle-LOCATION' );` Where LOCATION stands for your selected location -* **Parameters:** array (default classes), int (widget id) +* **Parameters:** + * array (default classes) + * string (widget id) + * string (sidebar id) + * array (widget data) == Installation == @@ -34,6 +42,9 @@ Allows you to change the HTML element for the subtitle == Changelog == += 1.1 = +* Add extra parameters to the filter hooks + = 1.0.1 = * Update textdomain hook diff --git a/widget-subtitles.php b/widget-subtitles.php index 80b3722..94226e6 100644 --- a/widget-subtitles.php +++ b/widget-subtitles.php @@ -2,13 +2,13 @@ /** * Plugin Name: Widget Subtitles * Description: Add a customizable subtitle to your widgets - * Plugin URI: http://github.com/JoryHogeveen/widget-subtitles - * Version: 1.0.1 - * Author: Jory Hogeveen - * Author URI: http://www.keraweb.nl + * Plugin URI: http://github.com/JoryHogeveen/widget-subtitles + * Version: 1.1 + * Author: Jory Hogeveen + * Author URI: http://www.keraweb.nl * Text Domain: widget-subtitles * Domain Path: /languages - * License: GNU General Public License v2 or later + * License: GNU General Public License v2 or later * License URI: http://www.gnu.org/licenses/gpl-2.0.html */ @@ -111,7 +111,7 @@ function in_widget_form( $widget, $return, $instance ) {

- locations as $locationKey => $locationName ) { ?> @@ -189,7 +189,7 @@ function widget_update_callback( $instance, $new_instance, $old_instance, $widge */ function dynamic_sidebar_params( $params ) { - global $wp_registered_widgets; + global $wp_registered_widgets, $_wp_sidebars_widgets; if ( ! isset( $params[0]['widget_id'] ) ) { return $params; @@ -206,19 +206,38 @@ function dynamic_sidebar_params( $params ) { // Check if there's an instance of the widget if ( array_key_exists( $params[1]['number'], $instance ) ) { - $instance = $instance[$params[1]['number']]; + $instance = $instance[ $params[1]['number'] ]; // Add the subtitle if ( ! empty( $instance['subtitle'] ) ) { + $sidebar_id = ''; + if ( is_array( $_wp_sidebars_widgets ) ) { + foreach ( $_wp_sidebars_widgets as $key => $widgets ) { + if ( in_array( $widget_id, $widgets ) ) { + $sidebar_id = $key; + break; + } + } + } + $subtitle_location = 'after-inside'; // default // Get location value if it exists and is valid if ( ! empty( $instance['subtitle_location'] ) && array_key_exists( $instance['subtitle_location'], $this->locations ) ) { $subtitle_location = $instance['subtitle_location']; } - // Filters subtitle element (default: span) - $subtitle_element = apply_filters( 'widget_subtitles_element', 'span', $widget_id ); + /** + * Filters subtitle element (default: span) + * @since 1.0 + * @since 1.1 Add extra parameters + * + * @param string 'span' The Element + * @param string $widget_id The widget ID (widget name + instance number) + * @param string $sidebar_id The sidebar ID where this widget is located + * @param array $widget All widget data + */ + $subtitle_element = apply_filters( 'widget_subtitles_element', 'span', $widget_id, $sidebar_id, $widget ); // Create subtitle classes $subtitle_classes = array( 'widget-subtitles', 'widgetsubtitle' ); @@ -228,8 +247,19 @@ function dynamic_sidebar_params( $params ) { foreach( $subtitle_location_classes as $location ) { $subtitle_classes[] = 'subtitle-' . $location; } - // Allow filter for subtitle classes to overwrite, remove or add classes - $subtitle_classes = apply_filters( 'widget_subtitles_classes', $subtitle_classes, $widget_id ); + + /** + * Allow filter for subtitle classes to overwrite, remove or add classes + * @since 1.0 + * @since 1.1 Add extra parameters + * + * @param array $subtitle_classes The default classes + * @param string $widget_id The widget ID (widget name + instance number) + * @param string $sidebar_id The sidebar ID where this widget is located + * @param array $widget All widget data + */ + $subtitle_classes = apply_filters( 'widget_subtitles_classes', $subtitle_classes, $widget_id, $sidebar_id, $widget ); + // Create class string to use $subtitle_classes = is_array( $subtitle_classes ) ? '' . implode( ' ', $subtitle_classes ) . '' : '';