Opened 9 years ago
Last modified 5 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)
Change History (5)
#2
@
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; } );
Note: See
TracTickets for help on using
tickets.
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: