Advertisement
fauzanjeg

Override Recent Comments Widget Output from WordPress Widget

Sep 19th, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.63 KB | None | 0 0
  1. /* START Override Recent Comments Widget Output from WordPress Widget */
  2. class WP_Widget_Recent_Comments_Custom extends WP_Widget_Recent_Comments {
  3.     function widget( $args, $instance ) {
  4.         static $first_instance = true;
  5.  
  6.         if ( ! isset( $args['widget_id'] ) ) {
  7.             $args['widget_id'] = $this->id;
  8.         }
  9.  
  10.         $output = '';
  11.  
  12.         $default_title = __( 'Recent Comments' );
  13.         $title         = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title;
  14.  
  15.         /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  16.         $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  17.  
  18.         $number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
  19.         if ( ! $number ) {
  20.             $number = 5;
  21.         }
  22.  
  23.         $comments = get_comments(
  24.             /**
  25.              * Filters the arguments for the Recent Comments widget.
  26.              *
  27.              * @since 3.4.0
  28.              * @since 4.9.0 Added the `$instance` parameter.
  29.              *
  30.              * @see WP_Comment_Query::query() for information on accepted arguments.
  31.              *
  32.              * @param array $comment_args An array of arguments used to retrieve the recent comments.
  33.              * @param array $instance     Array of settings for the current widget.
  34.              */
  35.             apply_filters(
  36.                 'widget_comments_args',
  37.                 array(
  38.                     'number'      => $number,
  39.                     'status'      => 'approve',
  40.                     'post_status' => 'publish',
  41.                 ),
  42.                 $instance
  43.             )
  44.         );
  45.  
  46.         $output .= $args['before_widget'];
  47.         if ( $title ) {
  48.             $output .= $args['before_title'] . $title . $args['after_title'];
  49.         }
  50.  
  51.         $recent_comments_id = ( $first_instance ) ? 'recentcomments' : "recentcomments-{$this->number}";
  52.         $first_instance     = false;
  53.  
  54.         $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
  55.  
  56.         /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
  57.         $format = apply_filters( 'navigation_widgets_format', $format );
  58.  
  59.         if ( 'html5' === $format ) {
  60.             // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
  61.             $title      = trim( strip_tags( $title ) );
  62.             $aria_label = $title ? $title : $default_title;
  63.             $output    .= '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
  64.         }
  65.  
  66.         $output .= '<ul id="' . esc_attr( $recent_comments_id ) . '">';
  67.         if ( is_array( $comments ) && $comments ) {
  68.             // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
  69.             $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
  70.             _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
  71.  
  72.             foreach ( (array) $comments as $comment ) {
  73.                 // var_dump($comment->comment_date);
  74.                 $output .= '<li class="recentcomments">';
  75.                 $output .= sprintf(
  76.                     /* translators: Comments widget. 1: Comment author, 2: Post link. */
  77.                     _x( '%1$s on %2$s', 'widgets' ),
  78.                     '<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>',
  79.                     '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>'
  80.                 );
  81.                 /* END CUSTOM || Show Date */
  82.                 $output .= '<br>';
  83.                 $output .= $comment->comment_date;
  84.                 /* END CUSTOM || Show Date */
  85.                 $output .= '</li>';
  86.             }
  87.         }
  88.         $output .= '</ul>';
  89.  
  90.         if ( 'html5' === $format ) {
  91.             $output .= '</nav>';
  92.         }
  93.  
  94.         $output .= $args['after_widget'];
  95.  
  96.         echo $output;
  97.     }
  98. }
  99.  
  100. add_action( 'widgets_init', 'register_custom_recent_comments' );
  101. function register_custom_recent_comments() {
  102.      register_widget( 'WP_Widget_Recent_Comments_Custom' );
  103. }
  104. /* END Override Recent Comments Widget Output from WordPress Widget */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement