Search
 
SCRIPT & CODE EXAMPLE
 

PHP

create widget to display comments in wordpress

<?php
  add_action( 'widgets_init', 'bb_load_widgets' );
 
function bb_load_widgets() {
    register_widget( 'bb_comments_Widget' );
}
 
class bb_comments_Widget extends WP_Widget {
 
    /**
     * Widget setup.
     */
    function bb_comments_Widget() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'bb_comments_Widget', 'description' => __('A widget that displays comments', 'bb_comments_Widget') );
 
        /* Widget control settings. */
        $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'bb_comments_Widget' );
 
        /* Create the widget. */
        $this->WP_Widget( 'bb_comments_Widget', __('GS bookshowcase reviews', 'bb_comments_Widget'), $widget_ops, $control_ops );
    }
 
    /**
     * How to display the widget on the screen.
     */
    function widget( $args, $instance ) {
        extract( $args );
 
        /* Our variables from the widget settings. */
        $title = apply_filters('widget_title', $instance['title'] );
        $number = $instance['number'];
 
        /* Before widget (defined by themes). */
        echo $before_widget;
 
        /* Display the widget title if one was input (before and after defined by themes). */
        if ( $title )
            echo '<h3 class="widget-title">' . $title . '</h4>';
 
        /* Get recent comments */
        $args = array(   
    'post_type' => 'gs_bookshowcase',
    'number' => $number,
    'orderby' => 'date',
    'order' => 'DESC'
  );

$comments = get_comments($args);
echo '<ol class="has-avatars has-dates has-excerpts wp-block-latest-comments">';
foreach($comments as $comment) :
                    
        ?>
         <li class="wp-block-latest-comments__comment">
                <?php echo wp_kses_post( get_avatar( $comment,$size = '60', '', '', array('class' => 'wp-block-latest-comments__comment-avatar') ) ); ?>
                <article><footer class="wp-block-latest-comments__comment-meta">
                    <a class="wp-block-latest-comments__comment-author" href="<?php echo get_comment_author_url();?>"><?php echo $comment->comment_author  ?></a> on <a class="wp-block-latest-comments__comment-link" href="<?php echo get_permalink($comment->comment_post_ID);?>#comment-<?php echo $comment->comment_ID; ?>"><?php echo get_the_title($comment->comment_post_ID); ?></a><time datetime="2022-02-28T19:42:47+08:00" class="wp-block-latest-comments__comment-date"><?php echo $comment->comment_date  ?></time></footer><div class="wp-block-latest-comments__comment-excerpt"><p><?php echo strip_tags($comment->comment_content); ?></p>
</div></article></li>



        <?php 
        endforeach;
        echo '</ol>';
 
        /* After widget (defined by themes). */
        echo $after_widget;
    }
 
    /**
     * Update the widget settings.
     */
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
 
        /* Strip tags for title and name to remove HTML (important for text inputs). */
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['number'] = strip_tags( $new_instance['number'] );
 
        return $instance;
    }
 
 
    function form( $instance ) {
 
        /* Set up some default widget settings. */
        $defaults = array( 'title' => __('GS Bookshowcase Reviews'), 'number' => __('5'));
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
 
        <!-- Widget Title: Text Input -->
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:90%;" />
        </p>
 
        <!-- Number of posts -->
        <p>
            <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Number of reviews to show:'); ?></label>
            <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" size="3" />
        </p>
    <?php
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: get current date from year input php 
Php :: insert into php myqsl 
Php :: php strings 
Php :: input if not null laravel 
Php :: Basic HTTP Authentication example 
Php :: What’s New in PHP 8 
Php :: default time of session in php 
Php :: php implode in html tags 
Php :: extract in php useful 
Php :: how to create object in php 
Php :: blocked token vs expired token 
Php :: laravel 9 requirements 
Php :: deleting a database in phpmyadmin 
Php :: php leggere file txt riga per riga 
Php :: preared request pdo 
Php :: php echo "<style" posts css text 
Php :: ezSql PDO Connection 
Php :: replace special characters from string in codeigniter 
Php :: compare in wp 
Php :: how get end of array in foreach php 
Php :: if order has product id in array 
Php :: php replace all text from string with associate array values 
Php :: Everything inside a pair 
Php :: How to automail in php 
Php :: $s = [1,2,3] for loop use in php 
Php :: how to create php message 00 
Php :: php blob to string 
Php :: how-to-add-pagination-in-search-results wordpress 
Php :: Call to undefined method AppModelsespece::links() laravel 8 
Php :: php execute script wait for response 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =