EduardET

Custom Our Authors Extra Widget

Jul 6th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.42 KB | None | 0 0
  1. class Custom_ET_Authors_Widget extends WP_Widget {
  2.  
  3.     function __construct() {
  4.         parent::__construct(
  5.             'dt_et_authors',
  6.             esc_html__( 'Custom Extra - Authors', 'extra' ),
  7.             array(
  8.                 'description' => esc_html__( 'Display a list of authors.', 'extra' ),
  9.             )
  10.         );
  11.     }
  12.  
  13.     public function widget( $args, $instance ) {
  14.         $query_args = array(
  15.             'who'     => 'authors',
  16.             'order'   => 'ASC',
  17.             'orderby' => 'display_name',
  18.         );
  19.  
  20.         $authors = get_users( $query_args );
  21.  
  22.         echo $args['before_widget'];
  23.         if ( ! empty( $instance['title'] ) ) {
  24.             echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
  25.         }
  26.         ?>
  27.         <div class="widget_content">
  28.             <ul class="widget_list">
  29.                 <?php
  30.                 foreach ( $authors as $author ) {
  31.                     $is_author = $author->divi_capabilities['administrator'];
  32.                     $count = count_user_posts( $author->ID );
  33.                     $count = sprintf( _n( '%d Post', '%d Posts', $count ), $count );
  34.                     $url = get_author_posts_url( $author->ID );
  35.                 ?>
  36.                 <?php if (!$is_author) {?>
  37.                     <li>
  38.                         <a href="<?php echo esc_url( $url ); ?>" class="widget_list_portrait" rel="author">
  39.                             <?php echo get_avatar( $author->ID, 150, 'mystery', esc_attr( $author->display_name ) ); ?>
  40.                         </a>
  41.                         <a href="<?php echo esc_url( $url ); ?>" class="widget_list_author">
  42.                             <h3 class="title"><?php echo esc_html( $author->display_name ); ?></h3>
  43.                             <span class="post-meta"><?php echo esc_html( $count ); ?></span>
  44.                         </a>
  45.                     </li>
  46.                 <?php } ?>
  47.                 <?php } ?>
  48.             </ul>
  49.         </div>
  50.         <?php
  51.         echo $args['after_widget'];
  52.     }
  53.  
  54.     public function form( $instance ) {
  55.         $title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : esc_html__( 'Our Authors', 'extra' );
  56.         ?>
  57.         <p>
  58.             <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'extra' ); ?></label>
  59.             <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  60.         </p>
  61.         <?php
  62.     }
  63.  
  64.     public function update( $new_instance, $old_instance ) {
  65.         $instance = array();
  66.         $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
  67.  
  68.         return $instance;
  69.     }
  70.  
  71. }
  72. function dt_load_widget() {
  73.     register_widget( 'Custom_ET_Authors_Widget' );
  74. }
  75. add_action( 'widgets_init', 'dt_load_widget' );
Add Comment
Please, Sign In to add comment