Shortcode? What is that?
Shortcodes still play a major role for developers in WordPress in 2020. Everyone who has ever deactivated a WordPress theme with a page builder based on it knows this. Deactivating Divi (affiliate link), for example, makes the content of posts and pages look like this:
Everything in square brackets is a [shortcode] and ensures that the content is specially processed by the theme. So with shortcodes in the post content, many page builders display multiple columns, sliders or buttons according to the configuration of the element.
Not all page builders work this way. Elementor (affiliate link), for example, stores the configuration of elements in the post’s meta information, the so-called post metas. This has the advantage that posts and pages are still readable even if Elementor is deactivated.
But shortcodes can do much more. They are executed on the server and can fetch information from the database or via interface from other websites, prepare it as desired and send it to the visitor’s browser.
Formatted Text in Custom Field as Source
Often we have to adapt to grown structures when we take over existing data in the context of a relaunch. In this case a Custom Field of the type Textarea with technical data exists in the following form:
We want to output this formatted text in the article as a pretty table containing technical data.
Custom Field? What is that?
In addition to the possibility of creating completely individual post types, we can also add any fields to existing post types. Individual post types are called Custom Post Types or CPTs, individual fields in the post type are Custom Fields. Plugins that can be used to create custom fields are ACF or JetEngine from Crocoblock.
Custom Fields are often used when editors need to be supported in that they can only enter certain data in designated fields without having to worry about formatting and presentation.
Now we need something to find the corresponding Custom Field technische_daten of the post in the metadata and output it formatted as a table.
The Best Shortcode Ever
In the function that is executed, we first use get_post_meta to retrieve the technische_daten field from the meta data, in which the corresponding textual input is stored.
If we are successful with this, we start to build our table in the variable $out, split the content of the field into lines using the line break \n with explode and iterate through all received lines with foreach, formatting the output with the table line format tr.
Finally, we return the collected output with return, so that our shortcode actually returns a table:
And that’s all the magic of using code to output formatted text in a Custom Field as a table.
But why no Gutenberg Block?
An alternative to the shortcode for formatted content display would be a Gutenberg block. The block has the disadvantage that it would only work in the Gutenberg context. Shortcodes, on the other hand, can be used universally, in Gutenberg, in the Classic Editor and also in any page builder.