Expertise Shortcode
Expertise Shortcode
Wordpress has a mechanism for adding ‘shortcodes’ authors can use in their articles through a plugin. In an earlier article I described how to get started with writing your own Wordpress plugins. In this article I’ll show how to add a handy shortcode to the plugin that generates code in the expert pages which allows people to register as experts in a certain area of expertise.
Functionality
Every time I create a new expertise page I try to write a brief introduction to the subject. I also want to add a button to this page. When people click on the button they should get a little popup window where they’re asked to indicate their expertise level. This button is mostly repetitive code which only differs in a few parameters (like ‘which expertise page are we on right now’)
Wordpress’ shortcode mechanism is not the best mechanism to get this done but I want to add it this way mostly for educative purposes.
Adding the Shortcode
It’s real simple to add the shortcode to our plugin:
add_shortcode('expertise', 'expertise_shortcode');
That’s really all there is to adding the shortcode. Ofcourse we still need some code to implement expert_shortcode():
function expertise_shortcode ($att) {
$expert = <<<EOM
<input type="button" value="I'm an expert!">
EOM;
return $expert;
}
After we added this code to our Expertise plugin, inserting [expertise] into our articles/pages should display a very basic button. Why you still see [expertise] in this article even though the plugin is currently active is because I used double [['s in the article.
The above is ofcourse the easiest thing I could think of to make an introduction from.
Hyperlinks
Besides adding a button on the expertise page, we could also use this shortcode to link to the expertise page from withing an article. We could ofcourse make an entirely new shortcode for that but it's not neccesary at all; We can add attributes to the shortcode. Let's agree first that we'll give every expertise page it's own 'expertise' custom field which holds the name of the expertise. Custom fields are easy to add in the 'Edit Page' screen. As the value for this 'expertise' field we choose a simple name like 'Linux'.
Now we'll add an attribute link to the expert shortcode so we can use it like [expertise link=Linux]. The expertiselink attribute is optional so if we don’t add it we’ll still get the ugly button but if we do add it, the plugin will look in Wordpress’ database to find the URL of the page that has custom field ‘expertise’ set to ‘Linux’ and display code for a hyperlink to that page with all the titles etc.
function expertise_shortcode ($atts) {
global $wpdb;
// Extract attributes and set defaults if not present.
extract(shortcode_atts(array(
'link' => 0,
), $atts));
if ($link) {
$expertpost = $wpdb->get_row("
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key='expertise'
AND wpostmeta.meta_value='$link'
AND wposts.post_status='publish'
AND wposts.post_type='page'
", ARRAY_A);
$url = get_permalink ($expertpost['ID']);
$expert = <<<EOM
<a href="$url" title="{$expertpost['post_title']}">{$expertpost['post_title']}</a>
EOM;
} else {
$expert = <<<EOM
<input type="button" value="I'm an expert!">
EOM;
}
return $expert;
}
That code looks a bit more complex already. First of all we’re using the shortcode_atts() function to check if the attribute link is set in $atts. If it’s not set, it gets the default value of 0. So whether link is set or not becomes an if-then-else construct that determines whether to show the ugly button or an equally ugly link. When $link is set it will be set to ‘Linux’ or something along those lines. We should also do some filtering on this later as it’s value is put into a SQL query and we don’t really want to encourage SQL-injection.
The SQL query uses $wpdb, hence the global $wpdb declaration at the top of the function. If everything works well the query should return a row with all the wp_posts columns set. This sample code doesn’t have error checking yet, which it really should have as the query could also return 0 rows (if the author made a typo, or the expertise page administrator has forgotten to add the expertise custom field).
Finally here’s an example using [expertise link=Wordpress], please go see our Wordpress page!
Read More
If you liked this article, maybe you want to read more in the Wordpress category:
- New 3DN Techblog
- Add Ajax to Expertise Plugin
- Checking Comments for Requests
- Adding sendmail/SMTP to experts plugin
- Periodic Expertise Events
Or if you have any questions about this article or Wordpress in general you might want to visit our Wordpress page.

Loading...