A fresh mix of code snippets and tutorials

Some Useful WordPress Shortcodes

Here is a nice collection of useful WordPress shortcodes, including cool functionality like displaying author lists, automatic links, subscribe links, current date, and more!

Display a list of authors

This function adds a shortcode that you can use to display a list of all post authors on your site:

// shortcode: list authors
function shapeSpace_list_authors() {
	$authors = wp_list_authors('optioncount=1&show_fullname=1&exclude_admin=0&hide_empty=1&echo=0');
	return $authors;
add_shortcode('list_authors', 'shapeSpace_list_authors');

Usage: [list_authors]

Output: A list of all post authors

Display a list of friends

You can add this function to your theme’s functions file to add a shortcode that displays a list of friends (via your site’s bookmarks):

// shortcode: list friends
function shapeSpace_list_friends() {
	$links = wp_list_bookmarks('categorize=0&echo=0&title_li=');
	return '<ul>'. $links .'</ul>';
add_shortcode('list_friends', 'shapeSpace_list_friends');

Usage: [list_friends]

Output: A list of all friends/bookmarks

Auto-linked URLs

This tasty little snippet adds a shortcode that outputs an auto-wrapped URL:

// shortcode: autolink url
function shapeSpace_autolink_url($atts, $content = null) {
	$content = trim($content);
	$link = '<a href="'. $content .'">'. $content .'</a>';
	return $link;
add_shortcode('url', 'shapeSpace_autolink_url');

Usage: [url]http://example.com/[/url]

Output: <a href="http://example.com/">http://example.com/</a>

Display a subscribe to feed link

Here is an easy shortcode that can save some time when inviting your readers to subscribe to your site’s RSS feed:

// shortcode: display subscribe link
function shapeSpace_subscribe_link() {
	$output = '<a target="_blank" href="https://wp-mix.com/feed/">Subscribe to WP-Mix</a>';
    return $output;
add_shortcode('subscribe', 'shapeSpace_subscribe_link');

Usage: [subscribe]

Output: <a target="_blank" href="https://wp-mix.com/feed/">Subscribe to WP-Mix</a>

Three for the road..

And here are three “micro” shortcodes for your WordPress enjoyment:

// user-agent shortcode for #WP @ https://m0n.co/s1
function ua() { $ua = mm_clean($_SERVER['HTTP_USER_AGENT']); return $ua; } add_shortcode('ua','ua'); // shortcode: [ua]

// current-date shortcode for #WP @ https://m0n.co/s2
function current_date() { return date('Y-m-d'); } add_shortcode('date','current_date'); // shortcode: [date]

// #WP home-link shortcode @ https://m0n.co/s3
function h(){ return '<a href="'.get_bloginfo("url").'">'.get_bloginfo("name").'</a>'; } add_shortcode('h','h'); // shortcode: [h]

Usage: [ua], [date], [h]

Output: Chrome or whatever user agent.., 2017-01-09, WP-Mix

I actually tweeted these shortcodes awhile back, so they’re each made of 140 characters or less, lol ;)


Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress