<title> now random!

After a bit of reading, guessing, frustration, cursing, and mind-altering substances; I have hacked together something that randomizes things the way I want.

The main hang-up was I kept looking for some documentation about exactly how to handle <title> within the WordPress theme code. There were mentions of actions and such…but it wasn’t documented in a way I understood. I started to remedy that by trying to crash-course even deeper in to WordPress, but at some point it hit me; what if the damn engine is smart enough to detect my <title> tag.

So I tried it…and that’s exactly what I saw happen. Where as the <title> tag had been generated by WordPress as part of the (I assume) wp_head function; adding my own just over-rode it automatically. Now that I think about it; I think the theme feature I kept reading about may have been to tell WP to check for my own title? Either way…I had custom tags; so from there it was just finding out what php code to hack together. The main things I wanted was for the <title> tag and the header tagline to match and for the random <title> to display only on the home page. What transpired after that was looking up PHP syntax and figuring out how to make the if statements work.

Here are the modifications I made to which files:

functions.php

function randomtext() { 
    $lines = file("/var/www/pickmy/taglines.txt") ; 
    return $lines[array_rand($lines)] ; 
} 

This function, inserted at the end of the file (or anywhere probably); basically reads the taglines.txt in the pickmy.org directory root (self-hosted bitches), and returns a random one. This is literally unchanged from the previous implementation.

header.php

<?php
/**
 * @package Amphibious
 */
global $description;
$description = randomtext();
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="profile" href="http://gmpg.org/xfn/11">
	<?php if(is_home()) : ?>
	<title> <?php echo $description; echo ' - pickmy.org ';?> </title>
	<?php endif; ?>
<?php wp_head(); ?>

This is the beginning of the header.php file; there are two main changes to pay attention to:

global $description;
$description = randomtext();
...
<?php if(is_home()) : ?>
	<title> <?php echo $description; echo ' - pickmy.org ';?> </title>
	<?php endif; ?>

Rendering the header seemed as good of a time as any to generate the random tagline, but before we can use it later; we’ve got to declare a global variable. Real WP devs are probably cringing, but whatever. Then we simply set this variable ($description) to whatever the randomtext() function returns.

The next chunk checks if the requested page is the home page; and generates the title from $description if it is. Otherwise, it skips all this. This ensures the random title only appears on the home-page; allowing WordPress’ default <title> to be used when on something that’s not the home page.

template-parts/site-branding.php

<?php
//$description = randomtext();
global $description;
if ( $description || is_customize_preview() ) :
?>
<p class="site-description">
<?php echo esc_html( $description ); ?>
</p>
<?php endif; ?>

The main difference compared to the previous version is we don’t need to call the randomtext() function; it’s called as part of every header. So all we need to do is declare that we want the global $description variable and display it in branding at the top of the page.

And that’s it. With just those few changes, the tagline under the site title in the header changes every load; and the <title> used for the home page matches. I don’t think my old theme even did that. Maybe it did…I’ll have to look in to it sometime.

Site comments disabled. Please use Twitter.