close-up image of a woman wearing a hoodie and glasses with bright lights reflected in the lenses

How to Add a Site Name Shortcode to WordPress

A cos­mic cloud of gas­es con­dens­es under its own grav­i­ty, form­ing a star. Gen­er­a­tions upon gen­er­a­tions of sin­gle-celled organ­isms lead to mul­ti-cel­lu­lar com­plex life. And your hum­ble blog has grown beyond its sem­i­nal inspiration.

Change hap­pens, and you don’t have to find your­self in a sit­u­a­tion where if you decide to change the name of your site, you’ll need to find and replace poten­tial­ly hun­dreds of men­tions of the old name to keep your brand­ing consistent.

Most of my online projects suf­fer from many iden­ti­ty crises as I refine over and over again what it is I want to do with my online pres­ence. To save myself all of the trou­ble of labo­ri­ous­ly updat­ing ref­er­ences to my site’s title through­out all of a its con­tent, I start­ed using an incred­i­bly sim­ple Word­Press short­code to do the work for me.

Installation

There are mul­ti­ple ways in which you can add this short­code to your Word­Press instal­la­tion, among them:

  • Add the fol­low­ing code block to your the­me’s functions.php file.
  • If you’re using The­sis 1.8.5, add the fol­low­ing code block to your the­me’s custom_functions.php file.
  • If you’re using a cus­tom plu­g­in to hold your site’s cus­tomiza­tions, add the code block to it.
  • Last, you can ignore the code block alto­geth­er by down­load­ing this code block as a Word­Press plu­g­in, which can then be uploaded via your site admin pan­el’s Plu­g­ins → Add New page. 

The Code

/**
 * [sitename] — Site name shortcode
 *
 * @via https://www.rickbeckman.org/log/sitename-shortcode/
 */
add_shortcode( 'sitename', function() { return get_bloginfo(); } );

Usage

Once installed, you can insert your site name into your con­tent by using [sitename] any­where where short­codes are processed, such as in any of your posts or pages.

Now, no mat­ter how many times you update your site’s title in Set­tings -> Gen­er­al, you can be assured that your con­tent will always refer to its cur­rent name!

If this short­code was use­ful to you, you might also appre­ci­ate one of my oth­er Word­Press tweaks, and of course, don’t for­get to like this page and share it with your fel­low WordPress’ers!

3 thoughts on “How to Add a Site Name Shortcode to WordPress”

  1. Great snip­pet — thank you for shar­ing! Only thing it does­n’t work for post titles. Any­way around that?

    1
    1. Hey, Ace! If I under­stand it cor­rect­ly, you’re want­i­ng to have a short­code that you can use to dis­play a post or page’s title in the content?

      If that’s right, try this instead of the code above:

      /**
       * [title] — Content title shortcode
       *
       * @via https://www.rickbeckman.org/log/sitename-shortcode/
       */
      add_shortcode( 'title', function() { return get_the_title(); } );

Leave a Comment

Your email address will not be published. Required fields are marked *

Use your Gravatar-enabled email address while commenting to automatically enhance your comment with some of Gravatar's open profile data.

Comments must be made in accordance with the comment policy. This site uses Akismet to reduce spam; learn how your comment data is processed.

You may use Markdown to format your comments; additionally, these HTML tags and attributes may be used: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Rick Beckman