Ecommerce Shopify WordPress Discussion

WordPress plugin adding firstname and lastname on registration

So i'm making a extension plugin FOR another plugin. What i want to achieve is that i upon registration they are required to add their Firstname and Lastname, so i added the fields for it, i did the errorchecks for it and everything worked when testing. But when adding the last step the actual updating the database when i click the register button i get critical errors, the user is added to the database, but no first or lastname. I assume i don't even get in the "function registration_save_user". Could someone help me on assisting me with my final hurdle, keep in mind i'm very new at all this so any improvements are welcomed. Thanks in advance. add_filter( 'plugins_loaded', array( 'FootballPoolRegisterNames', 'init_extension' ) ); class FootballPoolRegisterNames { public static function init_extension() { add_action( 'register_form', [__CLASS__, 'registration_form_extra_names'], null, 2 ); add_filter( 'registration_errors', [__CLASS__, 'registration_check_fields'], null, 3 ); add_action( 'user_register', [__CLASS__, 'registration_save_user'], null, 4 ); } //1. Add fields. Just the basic layout added before the dropdown. public static function registration_form_extra_names() { $first_name = ( ! empty( $_POST['first_name'] ) ) ? sanitize_text_field( $_POST['first_name'] ) : '';?> <p> <label for="first_name">Voornaam</label> <input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( $first_name ); ?>" size="25" autocomplete="voornaam" required="required"> </p> <?php $last_name = ( ! empty( $_POST['last_name'] ) ) ? sanitize_text_field( $_POST['last_name'] ) : '';?> <p> <label for="last_name">Achternaam</label> <input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr( $last_name ); ?>" size="25" autocomplete="achternaam" required="required"> </p> <?php } //2. Check fields private static function check_field( &$errors, $field, $message ) { if ( Football_Pool_Utils::post_string( $field ) === '' ) { $errors->add( "{$field}_error", "<strong>Fout:</strong> voer je {$message} in." ); } } public static function registration_check_fields( $errors ) { // Check the added fields self::check_field( $errors, 'first_name', 'voornaam' ); self::check_field( $errors, 'last_name', 'achternaam' ); return $errors; } //3. Save userdata public static function registration_save_user( $user_id , $first_name, $last_name) { if ( ! empty( $_POST['first_name'] ) && ! empty( $_POST['last_name'] )) { update_user_meta( $user_id, 'first_name', $first_name ); update_user_meta( $user_id, 'last_name', $last_name ); } } } have tried multiple times, but all of them where without first and lastname and had a critical error, but all of them posted to my database, so i assume the add_action( 'user_register', [__CLASS__, 'registration_save_user'], null, 4 ); and/or public static function registration_save_user( $user_id , $first_name, $last_name) { if ( ! empty( $_POST['first_name'] ) && ! empty( $_POST['last_name'] )) { update_user_meta( $user_id, 'first_name', $first_name ); update_user_meta( $user_id, 'last_name', $last_name ); } } Is completly wrong.
So as mentioned in the comments i let ChatGPT read my file and fix the code that was faulty (the saving to the database) and it corrected my code and told me what i did wrong. I made a big error in the add_action part on line 7, when trying to define 'user_register' it only expects 1 variable which is the $user_id i had "NULL, 4" because i didn't know any better and i'm bad. Then it spit out the following function which worked perfectly with the code i already provided without the $first_name and $last_name i previously tried to append. //3. Save userdata public static function registration_save_user( $user_id ) { if ( isset( $_POST['first_name'] ) && !empty( $_POST['first_name'] ) ) { update_user_meta( $user_id, 'first_name', sanitize_text_field( $_POST['first_name'] ) ); } if ( isset( $_POST['last_name'] ) && !empty( $_POST['last_name'] ) ) { update_user_meta( $user_id, 'last_name', sanitize_text_field( $_POST['last_name'] ) ); } } So everything works now thanks to ChatGPT lol! Thanks to all.

February 13, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.