Hook: on_user_save
avatar
Señor FAQ

¡Hola, amigos! I’m Señor FAQ, the mustached maestro of questions and answers! With my trusty glasses and a book of endless wisdom, I turn dudas into solutions. Soy el héroe de los curiosos and the champion of clarity.


Hook: on_user_save

The on_user_save hook is triggered when user data is being saved. It allows plugins to capture and store additional user-specific information dynamically.

Usage


DynamicHooks::executeHook('on_user_save', false, $page2, $jkp);
    

This hook is executed using the DynamicHooks::executeHook() method with the following parameters:

  • $page2 - The ID of the user being saved.
  • $jkp - The data being submitted, typically from a form.

Example Implementation


DynamicHooks::addHook('on_user_save', function ($userId, $data) use ($jakdb) {
    if (isset($data['company_name'])) {

        // Get the plugin ID for the "Marketplace" plugin
        $pluginId = $jakdb->get('plugins', 'id', ['name' => 'Your_Plugin_Name']);

        if ($pluginId) {
            // Prepare the data
            $metaValue = json_encode([
                'company_name' => $data['company_name'] ?? '',
                'address' => $data['address'] ?? '',
                'country' => $data['country'] ?? '',
                'tax_id' => $data['tax_id'] ?? '',
                'additional_info' => $data['additional_info'] ?? ''
            ]);

            // Check if the record already exists
            $exists = $jakdb->has('plugin_data', [
                'plugin_id' => $pluginId,
                'plugin_item_id' => $userId,
                'meta_key' => 'user_data'
            ]);

            if ($exists) {
                // Update existing record
                $jakdb->update('plugin_data', [
                    'meta_value' => $metaValue
                ], [
                    'plugin_id' => $pluginId,
                    'plugin_item_id' => $userId,
                    'meta_key' => 'user_data'
                ]);
            } else {
                // Insert new record
                $jakdb->insert('plugin_data', [
                    'plugin_id' => $pluginId,
                    'plugin_item_id' => $userId,
                    'meta_key' => 'user_data',
                    'meta_value' => $metaValue
                ]);
            }
        }
    }
}, 'your_plugin');
    

Best Practices

  • Ensure all required user data is validated before saving.
  • Use appropriate meta keys to avoid conflicts with other plugins.
  • Optimize database queries to maintain performance during the save operation.

Troubleshooting

  • Verify that the hook is registered under the correct namespace.
  • Ensure that the required data fields (e.g., company_name) are present in the post data.
  • Check for database errors when inserting or updating records.
Was this article helpful?
0 out of 0 found this helpful