Random Quotes from File (PHP Script)

Tag line demo

I wanted to show random lines from a list instead of a static tagline on top of the Homepage. I was searching for a while but only found outdated infos or plugins. Some PHP code I found worked, but ended up showing two lines of my text file at once. I tweaked the code a little (not that there’s much to work with) and ended up creating this little plugin. A “real” programmer probably would laugh at this. But I’m not at all a programmer and frankly quite pleased with myself.

This is the code for the PHP script.

<?php
/*
Plugin Name: Quotes
*/
function random_phrase () 
{ 
$quotes = file ("wp-content/plugins/quotes_plugin/quotes.txt");
$num = rand (1, intval (count ($quotes)));
echo $quotes[$num];
}
?>

I just added this function call into the header.php to load the random text bits from the file. I know that I probably should create child themes and stuff. But honestly I didn’t change that much in my theme. If it gets updated and my changes get overwritten, I know what to change back.

<?php random_phrase (); ?>

This is how it looks in the header.php from line 35.

<?php if ( is_front_page() && is_home() ) : ?>
	<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
	<?php random_phrase (); ?>
<?php else : ?>
	<p class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
	<?php random_phrase (); ?>
	<?php
endif;