News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

registering someone using the register hook with a given avatar

Started by Naytheet, September 26, 2023, 11:04:47 AM

Previous topic - Next topic

Naytheet

I can overwrite the avatar in $regOptions using integrate_register, instead of editing the subs-member.php file.

The problem is that my hook doesn't do the job with
// prepare the SMF registration with the gotten data
$regOptions = array(
'interface' => 'guest',
'username' => $nick,
'email' => $email,
'password' => $pass,
'password_check' => $pass,
'check_reserved_name' => true,
'check_password_strength' => false,
'check_email_ban' => true,
'send_welcome_email' => false,
'require' => 'nothing',
'is_activated' => 1,
    'avatar' =>  $picture,
);

require_once($sourcedir . '/Subs-Members.php');

// -> empty array
var_dump(call_integration_hook('integrate_register', $regOptions));

that would be cool if the hook would send me any error message instead of an empty array. Any idea of what I'm doing wrong?
In me sanitas et robur

Diego Andrés

How are you using integrate_register?
It provides $regOptions as a reference, so you should be able to modify its value for $regOptions['avatar'].

function register_hook(&$regOptions)
{
    $regOptions['avatar'] = 'new value';
}

SMF Tricks - Free & Premium Responsive Themes for SMF.

Naytheet

In me sanitas et robur

Diego Andrés

No, you shouldn't overwrite the variable. Other MODs might be appending data to it as well.
It is safer to just change those values you need, or create the needed elements.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Naytheet

Quote from: Diego Andrés on September 26, 2023, 02:25:41 PMHow are you using integrate_register?
It provides $regOptions as a reference, so you should be able to modify its value for $regOptions['avatar'].

function register_hook(&$regOptions)
{
    $regOptions['avatar'] = 'new value';
}


I tried your code, but it doesn't work, the registration doesn't save the avatar. Does it work for you? I'm using the latest SMF version.
In me sanitas et robur

Diego Andrés

It's missing the other key in my post.

$regOptions['register_vars']['avatar'] = 'new value';

And yes it does work.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Naytheet

Miracle indeed   :laugh:

Can you remind us where we can find the SMF vars list ?
In me sanitas et robur

Naytheet

I suppose I have to add global $picture; in the hook if I want to use a variable ? call_integration_hook('integrate_register', array(&$regOptions)); is reluctant to accept a new var in the array.
In me sanitas et robur

Diego Andrés

You're not supposed to add variables to the array.
Can't really help you much if you don't include the code you are having issues with, it would provide more details to understand you.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Naytheet

the hook file includes this function :

function registering($nick, $pass, $email, $picture) {
global $sourcedir;
global $user_settings, $smcFunc, $user_info, $regOptions;

// prepare the SMF registration with the gotten data
$regOptions['interface'] = 'guest';
$regOptions['username'] = $nick;
$regOptions['email'] = $email;
$regOptions['password'] = $pass;
$regOptions['password_check'] = $pass;
$regOptions['check_reserved_name'] = true;
$regOptions['check_password_strength'] = false;
$regOptions['check_email_ban'] = true;
$regOptions['send_welcome_email'] = false;
$regOptions['require'] = 'nothing';
$regOptions['is_activated'] = 1;

require_once($sourcedir . '/Subs-Members.php');

// proceed to registration
$id_member = registerMember($regOptions, true);

call_integration_hook('integrate_register', array(&$regOptions));

// (there are other steps to dologin(). $id_member's type will tell if the user already registered before.)


the hook function in the file :

/**
 * naytheet_create_avatar
 *
 * Called by integrate_register hook. Appends the SL avatar in the new user's profile.
 * @param $regOptions the reference provided containing the register vars.
 * @return void.
 */
function naytheet_create_avatar(&$regOptions) {
global $picture;
$regOptions['register_vars']['avatar'] = $picture;
}

I can add more if you need understand better. I'm not having any more issue with that, thanks to your help, I just wanted to know if that global var is needed, but it seems so.
In me sanitas et robur

Diego Andrés

I know understand a bit more, thank you.
The hook will be called when the new user is registered, you don't need to call it yourself, calling registerMember will trigger it and any other hook inside that function in Subs-Members.php.

What you could try is storing your avatar with a different name, when you have your data inside the registering function.
For example:
$regOptions['naytheet_social']['avatar'] = $picture;

And when the hook gets triggered by your other function, this value should be available inside $regOptions (you still need to check for it because remember, the hook will run wherever the function was called, not just for you).

function naytheet_create_avatar(&$regOptions) {
    if (!empty(
$regOptions['naytheet_social']['avatar']))
        
$regOptions['register_vars']['avatar'] = $regOptions['naytheet_social']['avatar'];
}


SMF Tricks - Free & Premium Responsive Themes for SMF.

Naytheet

In me sanitas et robur

Advertisement: