WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 2 years ago

#35475 reopened enhancement

New functions: `current_widget` and `current_widget_id`

Reported by: 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 6 years ago.

Download all attachments as: .zip

Change History (5)

#1 @sebastian.pisula
6 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
6 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
6 years ago

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

#4 @johnbillion
6 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.