Top Poster
#1 ramon fincken 399
#2 Site admin 3
#3 Ber|Art 2
#4 braddmark 1
#5 accentmedia 1
#6 Xarzu 1
#7 pEcosespedex 1
#8 khadish 1
#9 moxxx 1
#10 nistelrock 1
[tutorial] Phpbb3 creating a hook that is not existing/standard



Rating : 3 / 5

 
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding
Beta
Want to be notified by email when this topic gets a reply?  
View previous topic :: View next topic  
Author Message
ramon fincken
Site's programmer

Get a free globally recognized avatar
It's free!


Joined: 03 Aug 2007
Posts: 399
Location: A'dam/Diemen, The Netherlands

PostPosted: Sun Jun 21, 2009 5:45 pm    Post subject: [tutorial] Phpbb3 creating a hook that is not existing/standard Reply with quote

So today I needed to create a hook in my Phpbb3 install, to keep clear of the default phpbb3 coding as much as possible.

Beeing used to Wordpress coding this should be an easy task.
Manual:
http://area51.phpbb.com/docs/hook_system.html

But the darn thing wouldn't work.
Why?

Quote:
Pre-defined hookable phpBB3 functions

In phpBB3 there are four functions you are able to hook into with your custom functions:

phpbb_user_session_handler(); which is called within user::setup after the session and the user object is correctly initialized.
append_sid($url, $params = false, $is_amp = true, $session_id = false); which is called for building urls (appending the session id)
$template->display($handle, $include_once = true); which is called directly before outputting the (not-yet-compiled) template.
exit_handler(); which is called at the very end of phpBB3's execution.



Since i make use of Phpbb3's user system which is closely interconnected with my own custom coding ... I needed a function which deleted a few table entries for that user as well.
The easy and dirty way: Just add the code in the user_delete() function.


Now for a step by step way to create a NEW hook using as little phpbb3 core code changes.
This has been tested with a phpbb3 upgrade ( just merge the files ).
Alter the attached file/function to adapt to your liking for other hooks you want to create !

Step1:
Download the hook_user_delete.php file as txt: http://www.ramonfincken.com/uploads/topic_155.txt

Code:
COPY
hook_user_delete.php TO ./includes/hooks/hook_user_delete.php


(This file will be automatically included because of the hook_ prefix.)

Step2:
Code:
OPEN
./includes/functions_user.php


Code:
FIND
function user_delete


Code:
FIND
$phpEx;


Code:
AFTER, ADD

  // ---------------------------------
  // hook-my_custom_user_delete START
   global $phpbb_hook;
 if (!empty($phpbb_hook) && $phpbb_hook->call_hook(__FUNCTION__))
  {
     if ($phpbb_hook->hook_return(__FUNCTION__))
      {
        return $phpbb_hook->hook_return_result(__FUNCTION__);
    }
  }
  // hook-my_custom_user_delete END   
  // ---------------------------------
 


Step3:
Clear the cache ( ACP > index > clear cache )
OR
FTP to ./cache/ and delete the file data_hooks.php ( you can safely delete this file, it will be automatically re-created )


Now the only thing you have to do is look in the hook_user_delete.php file and adapt the function to your liking.

The core function:
Code:
/**
 * Phpbb3 addon ( hook ) to initiate actions when deleting a user from the ACP using the standard user_delete() function
 *
 * @author  Ramon Fincken, RamonFincken.com WebsiteFreelancers.nl
 */
function my_user_delete()
{
   global $cache, $config, $db, $user, $auth, $phpbb_hook; // Note: Not sure if you need phpbb_hook, probably not :)
   global $phpbb_root_path, $phpEx;
   
  // User to delete: $user_id
 $user_id = intval($_POST['u']); // Note: it would be nicer to have this as a param, but this works just fine

   if($user_id > 0)
  {   
     // Note: in my case I want to do some deletes, but feel free to
      // * call a function ( make sure phpbb has ACCESS to that function ! )
     // * do an insert in a "users_deleted" table, which can be read by your own code/CMS to perform actions later :)
   
     $sql = array();
      $sql[] = 'DELETE FROM some_table WHERE user_id = '.$user_id;
    $sql[] = 'UPDATE LOW_PRIORITY suggestions SET submitter = 0 WHERE submitter = '.$user_id;
 
     foreach($sql AS $key => $sql_temp)
     {
        $db->sql_query($sql_temp);
    }
  }
  return;
}




Read on/also interesting:
http://www.cs278.org/blog/2009/03/15/phpbb-post-queue/
Back to top
View user's profile Send private message Visit poster's website
Google adsense
Advertisement





PostPosted: Sun Jun 21, 2009 5:45 pm    Post subject: [tutorial] Phpbb3 creating a hook that is not existing/standard

Advertisement
Back to top
GravityForms
Advertisement





PostPosted: Sun Jun 21, 2009 5:45 pm    Post subject: [tutorial] Phpbb3 creating a hook that is not existing/standard

Advertisement
Gravity Forms Plugin for WordPress
Back to top
Post new topic   Reply to topic    Ramonfincken.com Forum Index -> PHP coding All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
General forums
The world of freelance
Programming
Other projects
Poll
I love gifts

Yes goodies and presents! [3]
No but I love polls ! [1]

Related google ads