[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.


Comments

2 responses to “[HOWTO] Dynamic Plugins for WordPress”

  1. 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

    1. 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….