Easy Way For Advertisement In WordPress

Since version 2.5 WordPress introduced the shortcode API, more detail about it in our article „WordPress Shortcode API“. Recently I had a task which is surely common, but with the help of shortcode API and custom fields very easy to solve. I had to include some advertisements (in german: Werbung) into some articles.

In each article the editor puts 3 custom fields: bild (image), link, linktext. In the text he can set the shortcode [ad]. This shortcode will be recognized by WordPress with the help of the following function. It will also load specific data to display the ads here.

Easy advertisement with shortcode-API in WordPress

Easy advertisement with shortcode-API in WordPress

The following function can be used as plugin or put in functions.php of your theme.

function example_link() {
	global $wp_query;
 
	$postID     = $wp_query->post->ID;
	$mylink     = get_post_custom_values('link', $postID);
	$mypic      = get_post_custom_values('bild', $postID);
	$mylinktext = get_post_custom_values('linktext', $postID);
 
	return '<a href="' . $mylink[0] . '"><img title="' . $mylinktext[0] . '" src="' . $mypic[0] . '" alt="" /></a>';
}
 
add_shortcode('werbung', 'example_link');

In your content it will display the image with the link and link text. This is just an example and there are many other possibilities to use this. Of course you could get the data for link, image and link text from the other data fields, but a shortcode API makes it easier to include something like this in their post.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">