How to Create a Slogan Randomizer Using PHP

One of my all-time favorite Saturday Night Live skits was “Stuart Smalley’s Daily Affirmations.

I’ve decided to have a little fun with PHP by adding random Stuart Smalley quotes to the gench site I’m building. I only want one user to see these quotes (my client who is also my husband) and it’s really just a silly joke between us. As silly as this example is, this technique can be used for serious applications as well. Many is the client who wants rotating slogans in their branding zone.

This example only displays the quotes on user admin pages and only for the “storekeeper” account. I’m replacing the pages’ titles with the quotes (but you could just as easily add the quotes to the page).

The following PHP function is placed in my theme’s template.php file…

function genchstyle_preprocess_page(&$variables, $user) {
/*Stuart Smalley's daily affirmation randomizer for store keeper (just for kicks)*/  
 global $user;
 $current_user = user_load($user->uid);
  /*Only display slogans for user 44 and on user admin pages*/
  if(($user->uid === '44') && (arg(0) === 'user')) {

  $slogans = array();
  $slogans[] = t('I\'m good enough, I\'m smart enough, and doggonit, people like me!');
  $slogans[] = t('I deserve good things.');
  $slogans[] = t('I am entitled to my share of happiness.');
  $slogans[] = t('I refuse to beat myself up.');
  $slogans[] = t('I\'m going to do a terrific show today!');
  $slogans[] = t('That\'s just stinkin\' thinkin!');
  $slogans[] = t('I am a human being, not a human doing.');
  $slogans[] = t('Trace it, face it, and erase it.');
  $slogans[] = t('Denial ain\'t just a river in Egypt!');
  $slogans[] = t('Compare and despair.');

  $slogan = $slogans[array_rand($slogans)];

  $variables['title'] = $slogan;

Once you’ve added this to your template.php file, you’ll need to clear all caches. As with all changes of this nature, make backups of files before editing them and implement & test the changes in a dev or test environment before introducing them to your live site.

Screen Shot 2013-10-08 at 4.38.01 PM

And don’t forget: “You’re good enough, you’re smart enough, and doggonit, people like you!”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s