The task is quite simple and is frequently discussed on blogs dedicated to WordPress and making money online. It is published with the same purpose as the previous one — to reference it from other articles instead of duplicating the same text ten times.
According to best practices (or at least to keep things neat), shortcodes are usually created in the “functions.php” file of the active WordPress theme. For example, if your site is using the “twentytwelve” theme, open the file located at “/wp-content/themes/twentytwelve/functions.php”. Use search to find the line “add_shortcode”, i.e. where existing shortcodes are located. Once found, scroll below the last existing shortcode and insert yours (this is done purely for neatness). Alternatively, scroll to the end of the file and add your shortcode there.
If desired, you can place your shortcodes in a separate file, implementing them as a plugin module, and include it regardless of the active theme.
The shortcode itself looks like this:
function wp2fl_shortcode($atts, $content, $shortcode)
{
return 'Hello World!';
}
add_shortcode('hello', 'wp2fl_shortcode');
Where:
hello — the name of the shortcode
wp2fl_shortcode — the function that implements the shortcode logic
WordPress passes three parameters to this function:
$atts — an array of attributes used (if any)
$content — the content of the shortcode (i.e., the text between [x]...[/x])
$shortcode — the name of the shortcode itself.
