I was checking some idea i had about writing a small user class with an option to ‘plug in’ functions for wordpress.

This page covers most of it :
dynamically add functions to php classes @ www.gen-x-design.com. The class construct at the end of the comment thread, Martin Pietschmann’s contribution, is rather useful. This pattern revolves around importing functionality from pluggable classes and exposing it through one object instance (the ‘decorator’ pattern mentioned is mostly used for writing extended classes, different objects with the same base data and functionality, ‘views’ sort of).

I can include the file with base, import and user class into function.php, and on making a user object have it read the directory and import functions modules (or load functionality conditional based on user role/authorization).

For this example I used the wordpress options table. I write a new functions class

class UserBogusPlugin extends MI_Importable
	public function the_anchor() {
//user_url and user_nicename are exposed through the user class
//I plug the functions class into, i can use the $this reference 
//as if i am writing code in the user class
                if($this->user_url<>'') {
                    return ''.$this->user_nicename .', ' . $this->first_name;
//no url, no anchor....   
		return $this->user_nicename;

…store the added class name in the options table…

//load $arr from options table
$modules = get_option('usermodules');
if($modules) $usermodules = json_decode($modules);
//add module
$usermodules[] = 'UserBogusPlugin';
//store back in options
add_option('usermodules', json_encode($usermodules)); 

…and load the plugin classes when instantiating the user object :

class User extends MI_Base
	public function __construct($ID) {
            $modules = get_option('usermodules');
            if($modules) {
                $usermodules = json_decode($modules);
                foreach($usermodules as $module) {
                  if(class_exists($module)) $this->import(new $module);
                 //$this->import(new $module);

In the wordpress template i can use the added functionality through the user instance :

      $my_user = new User($user_id);
      echo $my_user->the_anchor();

Fun with classes :)

add. 3-8 (qed) :
a function to load a directory with plugin files, i add a header /* plugin pluginfilename */ and check the files if there is a header.

       public function getPlugins()
            $plugdir = TEMPLATEPATH .'/plug';
            if ($handle = opendir($plugdir)) {
                $retval = array();
                while (false !== ($file = readdir($handle))) {
                    if (($file <> ".") && ($file <> "..")) {
                        $fh = fopen($plugdir.'/'.$file, 'r');
                        $contents = '';
                          $contents .= fread($fh, 1024);
                        if(preg_match('/lugin/', $contents))
                        {   //check for header : plugin, grab pluginname
                            $a = strpos($contents, 'lugin');
                            $a += 6;
                            $b = strpos($contents, ' ', $a);
                            $plugname = substr($contents, $a, $b-$a);
                            $retval[] = array($plugdir.'/'.$file, $plugname);
                }  }  }
            return $retval;

	public function __construct($id) {
//get he array with plugin files
            $usermodules = $this->getPlugins();
            if($usermodules) {
                foreach($usermodules as $module) {

                  //use require to load and import to add the function

                   $this->import(new $module[1]);    

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top