0

I am kind of new to WordPress coding. I'm trying to edit the wp-login.php file so if a user clicks the register button, it registers the user and stores the information in a different database table.

My code to insert new data into the database is below.

global $wpdb;
$wpdb->insert(
    'wp_classes',
    array(
        'userlogin' => $user_id, 
        'classselect' => $_POST['class_select']
    )
);
$wpdb->show_errors();
$wpdb->print_error();

Update - From what you said, I tried the code below, but no data was inserted into the database and no error.

add_action('user_register', 'store_class', 200);

function store_class($user_id) {
    global $wpdb;
    $wpdb->insert(
        'wp_classes',
        array( 
            'userlogin' => $user_id, 
            'classselect' => $_POST['class_select']
        )
    );
    $wpdb->show_errors();
    $wpdb->print_error();
}
  • How are you registering the user? Using default WordPress registration form? – Sisir Oct 15 '14 at 4:22
1

You can hook your function with user_register. It will fire immediately after an user registration is done. And user id is passed to hook as an argument.

add_action( 'user_register', 'my_custom_function' );
function my_custom_function ( $user_id  ) {
    //here goes your code
}
  • i tried this, but never got a my database inserted and no error, any other suggestions please? – Tobi Owolawi Jul 8 '14 at 9:47
  • Where did you write this code? It must be inside your functions.php file – тнє Sufi Jul 8 '14 at 18:16
  • works after putting the code inside functions.php file, for a reason i do not understand, since am new to wordpress code editing.... thank you very much – Tobi Owolawi Jul 9 '14 at 5:10
0

Write this code in your functions.php of your active theme or in your custom plugin.

<?php
add_action( 'user_register', 'my_insert_function' );

function my_insert_function ( $user_id  ) {
    //Write code to insert data in the seprate table 


}
?>

Before testing code directly, check by using die(); in this function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.