jump to navigation

Weekly Tip 1: Peacefully Degrading Template Tags March 3, 2006

Posted by weeklytips in Uncategorized.
trackback

Welcome to the first (well, so called weekly) WordPress tips post. This blog is dedicated to giving out tips to WordPress plugin developers and possibly even theme authors on little tricks you may or may not have known were possible with WordPress. Keep your eyes peeled every Friday for a guaranteed new tip, and occasionally days inbetween for some extras. And now stand by for this week’s tip.

A problem that many plugin developers face is having plugins that provide template tags that will gracefully fail when their plugin is removed. A majority of current plugins simply have a function that you call, similar to all of the other template tags, to place the output in your themes. The problem is that if a user uninstalls the plugin, they are now faced with a fatal PHP error in their theme because your function can no longer be found.

The solution to this problem is simple, but rarely used, probably because not many people outside of the mailing lists have seen the trick before. The solution? Hooks. Instead of telling a user to place a function call in their templates, you could tell them instead to copy and paste a little code snippet like this:

do_action(‘my_plugin_action’);

Alone, this call to do_action does nothing, and is similar to what you would find scattered about the WordPress code and themes that provide the standard set of hooks. But try adding a line in your plugin like this:

add_action(‘my_plugin_action’, ‘my_template_tag’);

Together, you’ve created a template tag to be copied and pasted into themes, that no longer kills sites when your plugin is deactivated. With this new template tag, you open up the doors for easy, clean integration with themes. Instead of trying to explain how to wrap your template tags in if(function_exists()) to the template designer who doesn’t understand PHP, they can once again copy and paste tiny snippets of code, providing a better user experience for everyone.

And as always, remember to prefix your functions and hook names with something unique, so that you don’t collide with other plugins.

Advertisements

Comments»

1. Ben - March 4, 2006

I have a question for ya. I’m trying to place my cat list in multiple colums like so in the header see http://forsalecallowner.com
————–
cat1 | cat4 | cat 7
cat2 | cat5 | cat 8
cat3 | cat6 | cat 9

and so on
1 Are there any themes that you know of that already do this?
2 What is the class or id name for list_cats()?
When I try to use this:

ul.top
{
width: 30em; /* room for 6 columns */
}
ul.top li
{
float: right;
width: 10em; /* accommodate the widest item */
}
/* stop the floating after the list */
br
{
clear: right;
}
/* separate the list from what follows it */
div.wrapper
{

margin-bottom: 1em;
}

/* anchor styling */
ul.top li a
{
display: block;
width: 7em;
text-decoration: none;
}
ul.top li a:hover
{
color: #FFF; /* white */
background-color: #A52A2A; /* brown */
}

or without the .top it doesnt work.
Sorry for the long post.

2. best movie porn - September 10, 2006

best movie porn

best movie porn

3. Weekly Tip 1: Peacefully Degrading Template Tags - January 10, 2010

[…] 1. Ben – March 4, 2006 […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: