[tutorial] Phpbb3 creating a hook that is not existing/standard

By.

min read

My profile

Share this:

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.
[b:a4c65e422d]Manual:[/b:a4c65e422d]
[url]http://area51.phpbb.com/docs/hook_system.html[/url]

But the darn thing wouldn’t work.
Why?

[quote:a4c65e422d]Pre-defined hookable phpBB3 functions

[u:a4c65e422d]In phpBB3 there are four functions[/u:a4c65e422d] 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.[/quote:a4c65e422d]

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 !

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

[code:1:a4c65e422d]COPY
hook_user_delete.php TO ./includes/hooks/hook_user_delete.php[/code:1:a4c65e422d]

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

[b:a4c65e422d]Step2:[/b:a4c65e422d]
[code:1:a4c65e422d]OPEN
./includes/functions_user.php[/code:1:a4c65e422d]

[code:1:a4c65e422d]FIND
function user_delete[/code:1:a4c65e422d]

[code:1:a4c65e422d]FIND
$phpEx;[/code:1:a4c65e422d]

[code:1:a4c65e422d]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
// ———————————

[/code:1:a4c65e422d]

[b:a4c65e422d]Step3:[/b:a4c65e422d]
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:1:a4c65e422d]/**
* 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;
}[/code:1:a4c65e422d]

[b:a4c65e422d]Read on/also interesting:[/b:a4c65e422d]
[url]http://www.cs278.org/blog/2009/03/15/phpbb-post-queue/[/url]

Share this:

Leave a Reply

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