While this site primary proposal is to offer wordpress themes, I decided it would be good (or of interest) for the readers of this blog to also write tips or even tutorials about Wordpress, its structure, the way it works and how to use, modify, improve or make themes.
The type of articles I intend to write are not obviously for developers or experienced theme makers but more for Wordpress enthusiasts who are interested in learning more about this CMS.
Today’s tutorial will be about controlling the appearance and the behavior of plugins by a few lines of code. This tip is really basic php coding and even if you’re allergic to this language, don’t be afraid, it will be smooth and easy;)
We’ll take as an example a plugin called “Simple Recent Comments” used by all my themes and that displays a nice list of the last comments posted on your blog. I must add before starting that this plugin is perfectly working and can be used with confidence.
Usually when you want to add a plugin to your blog you upload it to the right directory, activate it and add the following lines of code somewhere in your template:
<ul><?php src_simple_recent_comments(6,50,'', ''); ?></ul>This code tells the server to call the function “src_simple_recent_comments” and to execute the code (putting the recent comments in an unordered list). Here is what it looks like when everything goes well.
Now let say, something goes wrong. For example the plugin is not compatible with your current version or wordpress, you didn’t activate it (easy to fix) or the theme you developed was downloaded by somebody who doesn’t have the plugin or didn’t activate it. That results in a ugly parse error that usually destroys the layout of the site like here. What can you do from there? The only possibility you have so far is to quickly remove the code added before from your template. If it’s a theme used by somebody else, well, you’re stuck. Are you?
Luckily you’re not. And here the solution:
We just going to add a few more lines of code to make a conditional plugin activation.
<?php if (function_exists('src_simple_recent_comments')): ?> <br />
<div class="sidebartitle">Recent comments</div> <br />
<ul><?php src_simple_recent_comments(6,50,'', ''); ?></ul> <br />
<?php endif; ?>What does that tell you? Simply by adding these lines, The server will first look if the function (the plugin) exists and if it doesn’t it won’t run it! and it will look like that if you or your user didn’t activate it. The other advantage of such trick is that if your plugin doesn’t work or if you don’t want to use it anymore, you just have to desactivate it from the dashboard instead of removing physically the lines of code from the template.
Now that you get the idea, let’s complicate it a little bit. Say, you want to remind your users to activate the plugin. Here is a neat way to tell them:
<?php if (function_exists('src_simple_recent_comments')): ?> <br />
<div class="sidebartitle">Recent comments</div> <br />
<ul><?php src_simple_recent_comments(6,50,'', ''); ?></ul> <br />
<?php else: ?> <br />
<div class="sidebartitle">Recent comments</div>
Activate your plugin!
<?php endif; ?>This means that if the function is not active, the server will add a little comment “Activate your plugin!” instead as exemplified there. And this will disappear as soon as the plugin gets activated.
I hope that this little tutorial will be helpfull for some of you. Happy coding ;)

[…] Patrice over at XiAP Design has started a series of articles with the first about controlling your wordpress plugins. These articles are great for people to get under the hood of their wordpress and not be scared of all the intimidating code. Please be sure to check it out. […]