Select Page

In WordPress, shortcodes are special codes in square braces that are auto-magically replaced with pre-defined content. For example, I could place [kitten] in my post content and it could be replaced with a picture of a kitten. This short article explains how you can do it.

Before you can use a shortcode, you need to tell WordPress how to handle it when it meets it in your post or page content. To do that you need to go into your functions.php file in your theme. It’s possible you might even be able to edit it directly in your admin area unless you’ve locked down the security. Click on Appearance > Editor and click on Theme Functions in the list of files that are shown. and enter your code in there. I’ve already uploaded an image of a kitten to use. You’ll need to replace the image URL with one from your own website.

// Define the function to handle the shortcode
  function shortcode_kitten(){
      echo '<img src="/wp-content/uploads/2016/05/kitten.jpg" alt="kitten">';
  // Tell WordPress to call the function above when it sees [kitten]
  add_shortcode('kitten', 'shortcode_kitten');

Click on the Update File button at the bottom of the text area to save your changes.

Once you’ve done that, whenever you put [kitten] in a post or page, it will be replaced with a picture of a kitten.

See the official WordPress Codex page for more information about adding shortcodes to your site.