Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions widget.zone-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,15 @@ function widget( $args, $instance ) {

<?php if ( ! empty( $zone->description ) && $show_description ) : ?>
<p class="description"><?php echo esc_html( $zone->description ); ?></p>
<?php endif; ?>

<ul>
<?php foreach ( $posts as $post ) : ?>
<li>
<a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>">
<?php echo esc_html( get_the_title( $post->ID ) ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif;

if ( has_filter( 'widget_zone_posts_renderer' ) ) {
apply_filters('widget_zone_posts_renderer', $posts);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool idea. A better pattern I've found is to have false value passed to a filter and if unchanged, do the default render; something like:

$html = apply_filters( 'widget_zone_posts_override_html', false, $posts );
if ( $html !== false ) {
   echo $html;
} else {
   $this->widget_posts( $posts );
}

This way you're not echoing inside a filter which can be dangerous.

else {
$this->widget_posts( $posts );
}
?>

<?php echo wp_kses_post( $args['after_widget'] ); ?>
<?php
Expand All @@ -81,6 +79,21 @@ function widget( $args, $instance ) {
wp_cache_set( 'widget-zone-posts', $cache, 'widget' );
}

// filterable function to render posts in a zone post widget
function widget_posts( $posts ) {
?>
<ul>
<?php foreach ( $posts as $post ) : ?>
<li>
<a href="<?php echo esc_url( get_permalink( $post->ID ) ); ?>">
<?php echo esc_html( get_the_title( $post->ID ) ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php
}

function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$new_instance = wp_parse_args( (array) $new_instance, array( 'zone_id' => 0, 'show_description' => 0 ) );
Expand Down