Skip to content

1. Filter Hooks

Héctor Cabrera edited this page Mar 5, 2018 · 13 revisions

Filter hooks are one of the most versatile and useful features that WordPress has to offer. It allows maximum flexibility when it comes to modifying stuff, be it a theme, a plugin and even WordPress itself.

Recently takes advantage of this feature to let you modify the widget so it matches your specific needs. To use these filters, put them in your theme's functions.php file (or you can also create a plugin for this purpose, if you know how to).

Available filter hooks:

recently_custom_html

Since: 1.0.0.

The recently_custom_html filter hook gives you complete control over the HTML output of the widget.

This filter receives two parameters:

  • $recents, an array of recent posts objects.
  • $options, an array of options from the widget.

Keep in mind that the data you'll receive depends on what options you have enabled/disabled in the widget.

Here's an example:

/**
 * Builds custom HTML for the Recently plugin
 *
 * With this function, I can alter WPP's HTML output from my theme's functions.php.
 * This way, the modification is permanent even if the plugin gets updated.
 *
 * @param   array   $recents
 * @param   array   $options
 * @return  string
 */
function custom_recently_html_output( $recents, $options ){

    $output = '';

    if ( $recents->have_posts() ) {

        $output = '<ul class="recently-list">' . "\n";

        // Loop through items
        while ( $recents->have_posts() ) {

            $recents->the_post(); global $post;
            $stats = array(); // placeholder for the stats tag

            // Comment count option active, display comments count
            if ( $options['meta_tag']['comment_count'] ) {
                // display text in singular or plural, according to comments count
                $stats[] = '<span class="recently-comments">' . sprintf(
                    _n('1 comment', '%s comments', get_comments_number( get_the_ID() ), 'recently'),
                    number_format_i18n( get_comments_number( get_the_ID() ) )
                ) . '</span>';
            }

            // Pageviews option checked, display views count
            if ( $options['meta_tag']['views'] && function_exists('wpp_get_views') ) {
                // display text in singular or plural, according to views count
                $stats[] = '<span class="recently-views">' . sprintf(
                    _n('1 view', '%s views', intval( wpp_get_views( get_the_ID() ) ), 'recently'),
                    number_format_i18n( wpp_get_views( get_the_ID() ) )
                ) . '</span>';
            }

            // Author option checked
            if ( $options['meta_tag']['author'] ) {
                $author = get_the_author_meta( 'display_name', $post->post_author );
                $display_name = '<a href="' . get_author_posts_url( $post->post_author ) . '">' . $author . '</a>';
                $stats[] = '<span class="recently-author">' . sprintf(__('by %s', 'recently'), $display_name). '</span>';
            }

            // Date option checked
            if ( $options['meta_tag']['date']['active'] ) {
                $date = date_i18n( $options['meta_tag']['date']['format'], strtotime( $post->post_date ) );
                $stats[] = '<span class="recently-date">' . sprintf(__('posted on %s', 'recently'), $date) . '</span>';
            }

            // Taxonomy option checked
            if ( $options['meta_tag']['taxonomy']['active'] && !empty($options['meta_tag']['taxonomy']['names']) ) {
                $post_tax = array();
                // Get terms
                foreach( $options['meta_tag']['taxonomy']['names'] as $tax_name ) {
                    $terms = get_the_terms( $post->ID, $tax_name );
                    if ( !empty($terms) && !is_wp_error($terms) ) {
                        foreach ( $terms as $term ) {
                            $post_tax[$tax_name][] = '<a href="' . get_term_link( $term->slug, $tax_name ) . '">' . $term->name . '</a>';
                        }
                    }
                }

                if ( !empty($post_tax) ) {
                    $stats[] = '<span class="recently-taxonomy recently-category">under ' . implode(', ', $post_tax['category']) . '</span>';
                }
            }

            // Build stats tag
            if ( !empty($stats) ) {
                $stats = '<div class="recently-meta">' . join( ' &mdash; ', $stats ) . '</div>';
            }

            $output .= '
            <li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">
                <span itemprop="item" itemscope itemtype="http://schema.org/WebPage">
                    <a href="' . get_the_permalink( get_the_ID() ) . '" title="' . esc_attr( get_the_title() ) . '" itemprop="url" rel="bookmark"><span itemprop="name">' . get_the_title() . '</span></a>
                </span>
                ' . $stats . '
            </li>' . "\n";

        }

        $output .= '</ul>' . "\n";

    }

    return $output;

}
add_filter( 'recently_custom_html', 'custom_recently_html_output', 10, 2 );
Clone this wiki locally