Hook system

Discussion in 'PHP' started by stephan2307, Oct 6, 2010.

  1. #1
    Hi,

    I am writing a piece of software that I would later open for plugins. So for this reason I would like to implement a Hook System like the one that wordpress uses.

    How do I develop this? Can me anyone give me some advice?

    Thanks
     
    stephan2307, Oct 6, 2010 IP
  2. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #2
    You can use call_user_func_array or call_user_func
     
    guardian999, Oct 6, 2010 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    Thanks but how would I implement them?
     
    stephan2307, Oct 6, 2010 IP
  4. Deu

    Deu Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #4
    do you need to write an CMS or whatever which allow users to implement a plug-ins ?

    you can write a plugins module which will load a standard users plugins which it will override the system functionalities
     
    Deu, Oct 6, 2010 IP
  5. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #5
    This is all going a little bit over my head lol. Does anyone have any links to any tutorials or more in detail explanations?

    Thanks.
     
    stephan2307, Oct 6, 2010 IP
  6. ivan.kristianto

    ivan.kristianto Active Member

    Messages:
    136
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    if you want it like wordpress does, why don't you just browse into wordpress source and learn how they've done it.
     
    ivan.kristianto, Oct 6, 2010 IP
  7. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #7
    I tried that but wordpress is huge and at times looks quite messy. I will just develop the framework first and will add hooks later. :D
     
    stephan2307, Oct 6, 2010 IP
  8. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Ideally it should be link events & event registration.

    1. Write a function, which allows various modules to register to particular hooks.
    2. Another function to check the registered modules for a hook and call function supplied during registration.

    Best example of this can be found in MyBB forum software.
     
    mastermunj, Oct 6, 2010 IP
  9. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #9
    Wordpress kinda does it like this:

    First it includes all plugins with active hooks, regardless if they get called or not; I included this plugin as an example
    
    // in ./wp-content/plugins/save-remote-images/save-remote-images.php
    add_action('save_post', 'save_remote_images'); // "save_post" is the name of the hook that gets called after a successfully saved or updated post; "save_remote_images" is your function that gets called when that event fires
    function save_remote_images($postId) {
        // ...
    }
    
    PHP:
    Then when you save or update a post, this function gets called
    
    function save_remote_images ($postId)
    // in ./wp-includes/post.php
    function wp_insert_post($args) {
        // code that parses post data
        // ...
        // after it parses and saves the post
        do_action('save_post', $post_ID, $post); // this is the point where your hook gets called, in this case function "save_remote_images"
        return $post_ID;
    }
    
    PHP:
     
    Gray Fox, Oct 6, 2010 IP
  10. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You can try something like "Smarty Template" system?
     
    imperialDirectory, Oct 6, 2010 IP
  11. Monotoko

    Monotoko Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    All you would really need to do is check every so often that the plugins do not want anything. If they do, do the function they want to do then keep going. That's what Wordpress does I do believe.
     
    Monotoko, Oct 6, 2010 IP
  12. veroxii

    veroxii Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    veroxii, Oct 6, 2010 IP