Jump to content

how to build plugins system


pcman

Recommended Posts

I don't believe the question was 'How do I write a wordpress plugin?' But 'how do I write a plugin system?'

 

Unfortunately there is no simple answer to this question as it really depends on your overall applications design. Some applications (such as wordpress) have hooks that are executed at during certain phases of the applications executing. You can then use these hooks to inject third party code. Others use the filter chain pattern to allow injection into specific locations within the execution process. Others might use an observer pattern and broadcast events during execution.

 

There are lot's of ways of doing it. All of which are likely to depend entirely on how your application is designed int he first place. If your application is simply spaghetti code that is tightly coupled and not really using any clearly defined patterns then a plugin system will be quite difficult to implement.

 

Can you describe your architecture?

Link to comment
Share on other sites

  • 2 weeks later...

One method that I'm looking into is simply having a system that adds/replaces code on plugin install similar to how SMF, phpBB, etc do it (almost done, just need to get a code find/replace system setup). I don't know if this is the best method but I don't really know of any alternatives of this level. I think the benefit with this system would be you can create plugins that radically alter/add functionality.

 

Another method I looked into was having a plugin activate via a database entry and then have a file with code for that plugin load using a function. This system in theory should work great if you wanted to add modules and such to your site/CMS but if you wanted more advanced functionality, like for example a CAPTCHA system, I don't think it would be very capable.

 

These are pretty simple methods that I've played around with but I don't know how well they would reflect in the community.

Link to comment
Share on other sites

This kind of system is very easy to implement using simple patches, which is IMO what SMF should be using instead of there custom search and replace framework.

 

The only real problem with this method is that once you have installed a plugin that replaces existing code, another plugin that looks for that code won't be able to find it.

Link to comment
Share on other sites

This kind of system is very easy to implement using simple patches, which is IMO what SMF should be using instead of there custom search and replace framework.

 

The only real problem with this method is that once you have installed a plugin that replaces existing code, another plugin that looks for that code won't be able to find it.

 

Patches? Sorry, can you give an example of what you mean.

 

Valid point regarding that type of a system though.

Link to comment
Share on other sites

There is a program called diff that can display the difference between two files or directories or even an entire directory structure.

 

You can use the output of this diff program to create patch files. You can then use a patch file to apply these changes to code.

 

Example.

 

old/foo.php

<?php

echo "hello world";

 

new/foo.php

<?php

echo "Hello World";

 

Creating a patch:

 

diff -rupN old/ new/ > foo.patch

 

foo.patch

diff -rupN old/foo.php new/foo.php
--- old/foo.php 2011-08-15 11:10:14.000000000 +1000
+++ new/foo.php 2011-08-15 11:10:43.000000000 +1000
@@ -1,3 +1,3 @@
<?php

-echo "hello world";
+echo "Hello World";

 

The - at the start of  aline indicates that that line has been removed, while the + indicates that that line has been added.

 

To apply a patch you use the patch command:

 

cd old/; patch -p1 < ../foo.patch

 

Now, if you look at old/foo.php

<?php

echo "Hello World";

 

Patching can be even easier these days of your project uses Git for version control. Git has it's own mechanism for creating and applying patches.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.