WordPress Shortcode (Basics)

Introduction

WordPress shortcode on a page or post will be replaced with some content, and the process that happened in the background is as follows:

When WordPress “runs into” a shortcode it is instructed to look for a macro inside the brackets [ ], then the macro calls a callback function that will replace the shortcode with some dynamic content.

Enable shortcode everywhere

By default, WordPress ignores the shortcode if it finds it somewhere other than the part where the content of the post or page is entered. In order to activate shortcodes in other areas as well, it is necessary to add an appropriate filter to functions.php

Shortcode in widget

Now it is enough to put the shortcode in a regular Text widget and it in the area you want to display the content of the shortcode.

Shortcode in comments

Shortcode in excerpt

Shortcode in topic header

If for some reason we want to insert a shortcode inside a template or plugin, e.g. “custom page template” we use the function do_shortcode( $content )

Example

At the place inside the code where we want the shortcode to appear, add the following code:

Example

If shortcode with opening and closing tag:

Example

To assign to a variable what the shortcode returns:

Simple shortcode

Creating a shortcode

The procedure has the following flow:

  1. Create a callback function that will be called when eordpress “runs into” the shortcode
  2. Register the shortcode by giving it a unique name
  3. Hanging on the action hook

Callback function

This function does what shortcode is intended for, in the following example shortcode needs to “dump” the title of all posts written by the author “admin”:

NOTE:
The final “product” shortcode is text usually between HTML tags, to display it in the browser use return “string” and avoid the echo function. See more about this in the section of the article called “Printing HTML with a shortcode”.

Register shortcode

Registration is done with the function add_shortcode()

  • $tag – (string) (required) shortcode tag name inside brackets [ ]
  • $func – (required) Callback function that is called when wordpress encounters a shortcode

Hanging on a hook

Calling shortcode

Now it is possible for the article author, whenever he needs to list all his posts, just insert the shortcode [post_list] in any part of the post or static page. This list will be updated dynamically thanks to the php code in the background.

Shortcode with parameters

Introduction

When “creating” a shortcode with parameters, the following functions are used:

  • shortcode_atts() – wordpress function
  • extract() – PHP function

shortcode_atts()

This function allows a sequence of parameter/default_value pairs to be used within a shortcode (ie between brackets [ ] ).

  • $pairs – (required) array of pairs of type key/value (where value are default values)
  • atts – (required) name of the variable under which the string inside the shortcode tag will be used
  • $shortcode – (string) (optional) name of the shortcode that will be used
Example

In this example, the function creates an array of $atts pairs that is “linked” to the shortcode name in brackets [post_list]:
br_postova => 1
writer => ‘admin’

extract()

However the previously obtained string (key/value) must be “translated” into variables with associated default values.
The extract() function is used for this job. The variables are named $key and each has a default value associated with it. For shortcode purposes, this function is used in its simplest form and accepts only the string that needs to be changed as an attribute.

Example

If we assume that the string this function needs to process is the string from the previous example, then:

After processing this function created (although it is not visible anywhere) an array of variables to which values were added, when printed the array would look like this:
$br_postova = 1
$writer = ‘admin’

Callback function

If I want the shortcode to display a certain number of article titles from a certain article writer.

Registering and hooking is exactly the same as in the previous example:

Calling shortcode

Since we have defined default values, this code will work even without additional parameters, which means that the code will:

display one article (default value $br_posts=1) from the author of the article named ‘admin’ (default $writer is admin)

Now the user can select the article writer as well as the number of job titles to be displayed on one page:

Content in shortcode

If we want to insert the text before the list of articles from the previous examples, it is necessary to add a variable inside the callback function in the place where we want it to appear, but we also have to put it as a parameter that is passed to the function from the shortcode.

Calling shortcode

The content that is passed through the shortcode is placed between the opening and closing tags of the shortcode as in the following example:

The result of this code is the same as in the previous examples, except that now above the list there is a typed text styled as an h2 title.

Print HTML with shortcode

Explanation of the problem

When the author of an article uses a shortcode, he usually expects the shortcode to return some text. The callback function gives that text to the browser as HTML. When writing a callback function, you should use the return string variable and avoid the echo function. Text printed with echo can appear where we didn’t plan, because it sends the text directly to the page regardless of the function it’s in. Echo prints as soon as php “comes”to it and doesn’t wait for the function it’s in to end, while return returns a string and terminates the function! It may be better understood with the following examples:

Example (echo)

Example (return & echo)

Here is what they say about this on the official page:

“Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results.

Printing with return ‘string’

To gather all the HTML code that the shortcode should send to the browser, there are two approaches:

String concatenation

One way to “collapse” your HTML into a single variable is concatenation.

ob_start()

This function is used in conjunction with the supplementary function ob_get_clean(). From the moment it is placed in the code, it collects all the HTML code as well as everything that PHP prints with one of the functions for displaying on the screen. When it “runs into” the ob_get_clean() function in the code, it stops collecting and hands everything over to that function.

“Start remembering everything that would normally be outputted, but don’t quite do anything with it yet.”

Example

The

$var variable that ended up saving the entire buffer now has a value:

Example

This example is the same as in the section titled “Content in shortcode”, but instead of sting concatenation, the ob_start() and ob_get_clean() functions are used:

Hide discontinued shortcodes

In case the user changes the theme in which he used the shortcode, the new theme will not recognize them as a shortcode and will ignore them, i.e. it will treat them as plain text and print them as such. We can delete every “broken” shortcode:

  • manual i.e. to open every post where the shortcode was used and delete it.
  • by adding code to functions.php of the new theme:

    at the ‘shortcode_tag’ place, you should insert the name of each individual shortcode that you want to remove