Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

Code

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 add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.