0815

Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

WordPress Shortcodes

Save time by replacing your most commonly typed words and phrases with WordPress shortcodes. For example, if you are frequently typing your blog’s URL, you could place the following code your theme’s functions.php file:

function shortURL() {
	return 'https://www.08091971.de/';
}
add_shortcode('myurl', 'shortURL');

Now whenever I write a post via “HTML-mode”, all I need to include my blog’s URL is type “[myurl]”. Works great in WordPress 2.5 or better.

Shortcodes with Attributes
To create a shortcode for links, we need to include the href attribute information as well as the anchor text for the link itself. We can do this by placing this function in your theme’s functions.php file:

function shortLink($atts, $content = null) {
	extract(shortcode_atts(array(
		"href" => 'http://' // default URL
	), $atts));
	return '<a href="'.$href.'">'.$content.'</a>';
}
add_shortcode('link', 'shortLink');

Then, when creating a post, emulate the following format to include any links you wish:

[link href="https://www.08091971.de/"]Studio0815[/link]

..which will output the following code:

<a href="https://www.08091971.de/">Studio0815</a>

When the href attribute is removed from the shortcode, the default URL will be used. You may specify the default URL in the third line of the funtion (see comment).

Source: Digging into WordPress




0