• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

HANDS ON WORDPRESS

Making a Living with WordPress

  • Home
  • Blog
  • About
    • About Hands On WordPress
  • The Austin WordPress Community
  • Contact
  • Show Search
Hide Search
You are here: Home / WordPress / What are Shortcodes and When Do I Use Them

What are Shortcodes and When Do I Use Them

sandibatik · November 13, 2012 · Leave a Comment

Shortcodes are one of the most powerful features in WordPress

The WordPress codex summarized the use of shortcodes as: shortcode = shortcut.

WordPress Shortcode
WordPress shortcode

This does not begin to explain the power and usefulness of shortcodes. To begin, lets look at a few examples where we would use shortcodes:

  • Your theme requires specific content, such as call-outs or quotes to be wrapped in html, such as DIV tags with specific CSS classes. The TinyMCE editor in WordPress may strips those out, or non-technical users don’t remember to add the html, or do it wrong.
  • You want to display specific information, such as content from the logged-in user’s profile in a page or post. This information will not appear unless the user is logged in, and will be different for every user.
  • You want to retrieve information from another web site, like Twitter or Facebook, and display it inline with the normal content of a page or post.

These are just a few simple examples, but they serve to illustrate the range of possibilities.

A shortcode is a WordPress-specific code that can embed files or create objects that would normally require repetitive strings of HTML or CSS. A shortcode is a descriptive bit of text wrapped in square brackets, e.g. [donatebutton] – which can be inserted anywhere in your WordPress site. Shortcodes let authors and editors invoke functions by inserting a tag into the content of a page, post, or widget. There is virtually no limit to what can be done with shortcodes and their associated functions. Shortcodes are often dropped into your WordPress post to insert a ‘Call to Action’ button. When you implement shortcodes, you are able to call regularly used pieces of code in seconds, avoiding tedious repetition, while decreasing the chance of common, unforgiving typing errors.

To create a shortcode, you write a PHP function, which is a stand-alone block of code, typically placed in the functions.php file, that contains all the programming logic to do what you require. You then assign a name to it – its shortcode – so that it can be referenced inside a page, post, or widget.

Shortcodes can be very simple, or extremely complex. Anything that WordPress and PHP can do can be made into a shortcode.

What the user sees, however, is easy to remember and use. Shortcodes have 3 basic formats. You determine which format you want when the shortcode is written. These formats are:

[shortcodename]

[shortcodename option=”something”]

[shortcodename]Some text you want to be part of the formatting[/shortcodename]

When WordPress encounters a shortcode in a page, post, or widget, it passes any options or enclosed text to the function. That function code executes and returns the results, if any, back to WordPress, which then removes the shortcode and replaces it with the results.

Since being introduced with WordPress 2.5, the WordPress Shortcode API has become the go-to resource for fast and easy customization of site layout and inserting certain formatting snippets.  WordPress has a very easy function called do_shortcode() that lets you add shortcodes in your themes. For a detailed explanation of how to use shortcodes for creating and customizing WordPress themes and sample shortcodes go to: http://codex.wordpress.org/Function_Reference/do_shortcode

For specific shortcode snippets and how to use them go to:.https://handsonwp.com/category/knowledge-base/wp-snippets/shortcodes/

Filed Under: WordPress Tagged With: php function, Shortcodes

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About Sandi Batik

About Sandi Batik

Introverted Freelancer, WordPress trainer, consultant, curricula developer, author, unapologetic geek, unrepentant capitalist, lucky enough to do what I love … more about me about About Sandi Batik

  • Twitter

Search

Introverted Freelancer

Traits of Successful Introverted Freelancers

Traits of Successful Introverted Freelancers

2017 Business Check-up Workshop

2017 Business Check-up Workshop

Expanding Your Business With Automated Marketing Funnels

Expanding Your Business With Automated Marketing Funnels

How to Use Permission Marketing to Build Your WordPress Business

How to Use Permission Marketing to Build Your WordPress Business

How Much Should I Charge for Building or Designing a WordPress Website?

How Much Should I Charge for Building or Designing a WordPress Website?

Project Management

Keeping Scope Creep From Killing Your Schedule and Profit Margin

Keeping Scope Creep From Killing Your Schedule and Profit Margin

Project Management for WordPress Freelancers

Project Management for WordPress Freelancers

WordPress

Securing and Maintaining Your WordPress Site

Securing and Maintaining Your WordPress Site

How The WordPress Media Library Works — 2018

How The WordPress Media Library Works — 2018

How To Build an Information Structure for Your WordPress Site

How To Build an Information Structure for Your WordPress Site

How WordPress Themes Really Work

How WordPress Themes Really Work

How to Create and Manage eMail Newsletters from Your WordPress Site

How to Create and Manage eMail Newsletters from Your WordPress Site

How to Secure and Maintain Your WordPress Site

How to Secure and Maintain Your WordPress Site

Copyright © 2010-2023 Hands On WordPress · All Rights Reserved