Make WordPress Core

Opened 9 years ago

Last modified 5 years ago

#35475 reopened enhancement

New functions: `current_widget` and `current_widget_id`

Reported by: sebastianpisula's profile sebastian.pisula Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Widgets Keywords: reporter-feedback has-patch
Focuses: Cc:

Description

This is alternative for ticket #35456. In widgets we can use different filters and actions. For example we use the_title filter but in widget we don't want (this is abstract example). So:

<?php
if(current_widget() == 'archive'){
return 'test';
}

return 'test2';

Other example - when can use filter from #35456 ticket:

<?php

add_filter( 'widget_archives_args', function ( $args ) {

        if ( current_widget_id() == 'archives-2' ) {
                $args['post_type'] = 'page';
        }

        return $args;
}, 10 );

Attachments (1)

35475.patch (3.9 KB) - added by sebastian.pisula 9 years ago.

Download all attachments as: .zip

Change History (5)

#1 @sebastian.pisula
9 years ago

Other example: I have widget Recent Posts. In current widget is title + date. But I need "date + excerpt". So in widget settings disable show date and add filter for the_title and if check:

<?php

if(current_widget() == 'recent_posts'){
return 'date '. title;
}

return $title;

#2 @westonruter
9 years ago

  • Keywords reporter-feedback close added

I don't think this is the right approach, to add a new global function. I believe there is already a way to do what you want, for example this plugin code:

<?php
$is_recent_posts_widget = false;

add_action( 'dynamic_sidebar', function( $widget ) use ( &$is_recent_posts_widget ) {
    $is_recent_posts_widget = ( 0 === strpos( $widget['id'], 'recent_posts-' ) );
} );

add_filter( 'widget_archives_args', function ( $args ) use ( $is_recent_posts_widget ) {
    if ( $is_recent_posts_widget ) {
        $args['post_type'] = 'page';
    }
    return $args;
} );

#3 @westonruter
9 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

#4 @johnbillion
9 years ago

  • Keywords has-patch added; close removed
  • Milestone set to Awaiting Review
  • Resolution wontfix deleted
  • Status changed from closed to reopened

I don't see the objection to this. The code suggested by Weston is pretty gnarly and is tightly coupled to the internally generated widget ID.

Note: See TracTickets for help on using tickets.