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 '';
}

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.


Posted

in

by

Comments

7 responses to “Easy Way For Advertisement In WordPress”

  1. […] Easy way to advertise in WordPress using shortcodes Great resource to manage and insert advertising on your blog, brought to you by WpEngineer. […]

  2. Mehdi Avatar

    Great and easy!!!

  3. […] Easy way to advertise in WordPress using shortcodes Great resource to manage and insert advertising on your blog, brought to you by WpEngineer. […]

  4. Daichi Avatar
    Daichi

    Thank you very much. Nice n easy

  5. […] Easy way to advertise in WordPress using shortcodes Great resource to manage and insert advertising on your blog, brought to you by WpEngineer. […]

  6. […] Easy way to advertise in WordPress using shortcodes Great resource to manage and insert advertising on your blog, brought to you by WpEngineer. […]

  7. Tom Coady Avatar

    When you say “The following function can be used as Plugin or put in functions.php of your theme.” is this an optional step? I got as far as adding the custom field but it renders in preview as [customid] so I’m not sure if I also need to add code to functions.php

    Also there’s a plugin that enables this where you define any text with a code like :this: and it will automatically display whatever that’s defined to, which for me is a bit simpler than editing the php, assuming this is necessary.