# Random Quote Widget

> Adds a "Random Quote" widget that shows a different quote, tip or testimonial from your list on each page view, with the source shown below it.

- Type: PHP
- Runs: Run everywhere
- Category: [Widgets](https://wpide.com/library/category/widgets/)
- Tags: widgets, quotes, testimonials
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/random-quote-widget/

## Code

```php
if ( ! class_exists( 'WPIDE_Random_Quote_Widget' ) ) {
    class WPIDE_Random_Quote_Widget extends WP_Widget {
        public function __construct() {
            parent::__construct( 'wpide_random_quote', 'Random Quote', array(
                'description'                 => 'Shows one quote picked at random from your list on each page view.',
                'customize_selective_refresh' => true,
            ) );
        }

        public function widget( $args, $instance ) {
            $lines = isset( $instance['quotes'] ) ? array_values( array_filter( array_map( 'trim', explode( "\n", $instance['quotes'] ) ) ) ) : array();

            if ( ! $lines ) {
                return;
            }

            // "Quote text — Who said it": the part after the last " — " or " - " is the source.
            $parts  = preg_split( '/\s+[—–-]\s+(?=[^—–-]*$)/u', $lines[ array_rand( $lines ) ], 2 );
            $quote  = $parts[0];
            $source = isset( $parts[1] ) ? $parts[1] : '';
            $title  = isset( $instance['title'] ) ? $instance['title'] : '';
            $title  = apply_filters( 'widget_title', $title, $instance, $this->id_base );

            echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput -- theme markup.

            if ( '' !== (string) $title ) {
                echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput
            }

            echo '<figure class="wpide-random-quote" style="margin:0;"><blockquote style="margin:0;"><p>' . esc_html( $quote ) . '</p></blockquote>';

            if ( '' !== $source ) {
                echo '<figcaption>— ' . esc_html( $source ) . '</figcaption>';
            }

            echo '</figure>';
            echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput
        }

        public function form( $instance ) {
            $title  = isset( $instance['title'] ) ? $instance['title'] : '';
            $quotes = isset( $instance['quotes'] ) ? $instance['quotes'] : '';

            echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">Title:</label><input class="widefat" type="text" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" value="' . esc_attr( $title ) . '" /></p>';
            echo '<p><label for="' . esc_attr( $this->get_field_id( 'quotes' ) ) . '">Quotes, one per line ("Quote — Source"):</label><textarea class="widefat" rows="8" id="' . esc_attr( $this->get_field_id( 'quotes' ) ) . '" name="' . esc_attr( $this->get_field_name( 'quotes' ) ) . '">' . esc_textarea( $quotes ) . '</textarea></p>';

            return '';
        }

        public function update( $new_instance, $old_instance ) {
            return array(
                'title'  => isset( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '',
                'quotes' => isset( $new_instance['quotes'] ) ? sanitize_textarea_field( $new_instance['quotes'] ) : '',
            );
        }
    }
}

add_action( 'widgets_init', function () {
    register_widget( 'WPIDE_Random_Quote_Widget' );
} );
```

## Notes

Write one quote per line as "Quote — Source"; with a page cache, the quote changes when the cache refreshes.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Random Quote Widget" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
