[HOWTO] Dynamic Plugins for WordPress

Problem: Installed the hemingwayEx theme. Looks great, but the pagination doesn’t work well without the custom hemingwayEx pagination plugin. I got that installed. The hemmingwayEx theme looks cool when it displays two posts per page. The plugin worked well except that for my other two themes, displayed two posts per page. I posted a topic at WordPress Forums. No results.

Solution: Just at the beginning of the hemingwayEx theme, I modified it to create a cookie, like this:

<?php
if(!isset($_COOKIE["wptheme" . COOKIEHASH])) {
		$expire=time()+3600;
		$ctheme="hemingwayEx";
		setcookie("wptheme".COOKIEHASH, stripslashes($ctheme), $expire);
}
?>


That’s the exact same cookie that “Theme Switcher Reloaded” creates. So it checks if the cookie is created, if not, it will create one.

Then add a condition before the plugin code.

<?php
function getTheme() {
	if (isset($_COOKIE["wptheme" . COOKIEHASH])) {
		return $_COOKIE["wptheme" . COOKIEHASH];
	}
}
$ctheme=getTheme();
if ($ctheme=="hemingwayEx") {
// put the rest of the plugin code here (everything)
}
?>

That’s it! Use this technique to enable any plugin only for certain themes.

This entry was posted on Thursday, May 14th, 2009 at 8:55 pm and is filed under another snippet | code. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

  • http://www.jaimineetak.webs.com jaimini tak

    hi Jude
    do u remember me
    i am Jaiminee Tak
    and i wanted to know that how can i improve my website?
    can u help me in blue-j

    • http://judepereira.com/blog Pereira Jude

      You’ve either got the wrong “Jude”, or I’ve lost my memory! Never heard of a Jaimini Tak, nor does the name ring a bell.
      Anyway, I’d be more than glad to help you in BlueJ, as I’ve used it myself for two years….