Jump to content

Plugin system in PHP


dpacmittal

Recommended Posts

I want to create a plugin system similar to one implemented in wordpress. Google hasn't been of any help. I tried reading wordpress source but it would take much time to understand the underlying concept. I've checked its database and it doesn't store any plugin information in the database which means that it parses all the plugins everytime the page is loaded which, in my opinion, is not (?) a good way to do it.

 

The system they've used is the hook system. There are hooks in most of the places. You add some action to the hook and when the hook is called, the action to attached is also executed. For eg; if you attach an action of cleaning a certain folder on the hook of add-post (say), everytime you add a post, the folder will be cleared.

 

Okay, so I've not been able to understand how wordpress does it. However, I myself have a basic idea for this. I just want opinions if it is a good way to do it. I've thought of something like this:

 

Plugins would have to call a function to attach an action to hook eg;

attach_action ($hookname, $callback)

 

When the plugin is installed, attach_action would add this information to the database.

 

When the hook is called, it fetches all the actions attached to it from the database and executes them.

 

When the plugin is uninstalled, the record from database is removed.

 

 

So this is it. I want to know if this is better/worse way than how wordpress does? Thanks

Link to comment
Share on other sites

  • 2 weeks later...

function thirdparty_linkification($content) {
     //..
}

plugin_register('news_content', 'thirdparty_linkification');

 

Assum a third-party developer wrote this. His plugin will linkify e-mails and http links in news articles more specifically in the content (body) of the article. I as a developer then write:

 

function cms_get_latest_news($number = 10) {
    //..
    while ($article = mysql_fetch_assoc($result)) {
        $article['content'] = plugin_apply('news_content', $article['content']);
    }
    return $articles;
}

 

This will give each and every registered plugin a chance of doing what it wants to do (linkify, footer, ..) I could expand this by adding for example news_time which will give every plugin a chance of telling how it should be formatted and so on.

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.