Make WordPress Core

Opened 8 years ago

Last modified 6 years ago

#38031 new enhancement

Allow modifying args for the_widget

Reported by: mnmlthms's profile mnmlthms Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Widgets Keywords: has-patch
Focuses: ui Cc:

Description (last modified by westonruter)

Some plugins used function the_widget outside of sidebar with default args, this enhancement should be added to the next version so theme developers can modify args

Original code:

<?php
/**
 * Output an arbitrary widget as a template tag.
 *
 * @since 2.8.0
 *
 * @global WP_Widget_Factory $wp_widget_factory
 *
 * @param string $widget   The widget's PHP class name (see class-wp-widget.php).
 * @param array  $instance Optional. The widget's instance settings. Default empty array.
 * @param array  $args {
 *     Optional. Array of arguments to configure the display of the widget.
 *
 *     @type string $before_widget HTML content that will be prepended to the widget's HTML output.
 *                                 Default `<div class="widget %s">`, where `%s` is the widget's class name.
 *     @type string $after_widget  HTML content that will be appended to the widget's HTML output.
 *                                 Default `</div>`.
 *     @type string $before_title  HTML content that will be prepended to the widget's title when displayed.
 *                                 Default `<h2 class="widgettitle">`.
 *     @type string $after_title   HTML content that will be appended to the widget's title when displayed.
 *                                 Default `</h2>`.
 * }
 */
function the_widget( $widget, $instance = array(), $args = array() ) {
        global $wp_widget_factory;

        $widget_obj = $wp_widget_factory->widgets[$widget];
        if ( ! ( $widget_obj instanceof WP_Widget ) ) {
                return;
        }

        $default_args = array(
                'before_widget' => '<div class="widget %s">',
                'after_widget'  => "</div>",
                'before_title'  => '<h2 class="widgettitle">',
                'after_title'   => '</h2>',
        );
        $args = wp_parse_args( $args, $default_args );
        $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] );

        $instance = wp_parse_args($instance);

        /**
         * Fires before rendering the requested widget.
         *
         * @since 3.0.0
         *
         * @param string $widget   The widget's class name.
         * @param array  $instance The current widget instance's settings.
         * @param array  $args     An array of the widget's sidebar arguments.
         */
        do_action( 'the_widget', $widget, $instance, $args );

        $widget_obj->_set(-1);
        $widget_obj->widget($args, $instance);
}

Enhanced

<?php
/**
 * Output an arbitrary widget as a template tag.
 *
 * @since 2.8.0
 *
 * @global WP_Widget_Factory $wp_widget_factory
 *
 * @param string $widget   The widget's PHP class name (see class-wp-widget.php).
 * @param array  $instance Optional. The widget's instance settings. Default empty array.
 * @param array  $args {
 *     Optional. Array of arguments to configure the display of the widget.
 *
 *     @type string $before_widget HTML content that will be prepended to the widget's HTML output.
 *                                 Default `<div class="widget %s">`, where `%s` is the widget's class name.
 *     @type string $after_widget  HTML content that will be appended to the widget's HTML output.
 *                                 Default `</div>`.
 *     @type string $before_title  HTML content that will be prepended to the widget's title when displayed.
 *                                 Default `<h2 class="widgettitle">`.
 *     @type string $after_title   HTML content that will be appended to the widget's title when displayed.
 *                                 Default `</h2>`.
 * }
 */
function the_widget( $widget, $instance = array(), $args = array() ) {
        global $wp_widget_factory;

        $widget_obj = $wp_widget_factory->widgets[$widget];
        if ( ! ( $widget_obj instanceof WP_Widget ) ) {
                return;
        }

        $default_args = array(
                'before_widget' => '<div class="widget %s">',
                'after_widget'  => "</div>",
                'before_title'  => '<h2 class="widgettitle">',
                'after_title'   => '</h2>',
        );
        $args = wp_parse_args( $args, $default_args );

        //Allow modifying widget args via filter hook 
        $args = apply_filters( 'the_widget_args', $args );
        $args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] );

        $instance = wp_parse_args($instance);

        /**
         * Fires before rendering the requested widget.
         *
         * @since 3.0.0
         *
         * @param string $widget   The widget's class name.
         * @param array  $instance The current widget instance's settings.
         * @param array  $args     An array of the widget's sidebar arguments.
         */
        do_action( 'the_widget', $widget, $instance, $args );

        $widget_obj->_set(-1);
        $widget_obj->widget($args, $instance);
}

Attachments (1)

38031.diff (622 bytes) - added by odminstudios 6 years ago.

Download all attachments as: .zip

Change History (5)

#1 @westonruter
8 years ago

  • Description modified (diff)

#2 @joedolson
8 years ago

  • Focuses accessibility removed

#3 @desrosj
6 years ago

  • Keywords needs-patch added

@mnmlthms Thanks for this ticket, and welcome to Trac! Apologies that it took so long to receive a response.

Is this still of interest to you? I think the request for a filter here is reasonable, but can you detail a use case? This also needs a patch.

@odminstudios
6 years ago

#4 @odminstudios
6 years ago

  • Keywords has-patch added; needs-patch removed
  • Version 4.7 deleted

Uloaded the patch.
I've changed the code @mnmlthms suggested, so it would have isset check.

Note: See TracTickets for help on using tickets.