The MU forums have moved to WordPress.org

Custom registration fields: Move wp_signups "meta" into wp_usermeta (2 posts)

  1. jlm99
    Member
    Posted 4 years ago #

    Hi! I'm adding custom fields for user registration, and I've got the fields saving in the "meta" column of the wp_signups table, but I can't figure out how to move that "meta" array over to wp_usermeta when the user activates. Here's what I'm trying, but it causes a "Unknown column" database error (even though I can see the data in MySQL), and it doesn't save the new fields to wp_usermeta:

    ...
    add_action('user_register', 'usas_reg_usermeta'); // Hooks to end of wp_insert_user function in wp-includes/registration.php
    ...
    function usas_reg_usermeta($user_id) {
      global $wpdb;
      $new_userinfo = get_userdata($user_id);
      $new_username = $new_userinfo->user_login;
      $new_usermeta = $wpdb->get_results("SELECT meta FROM $wpdb->signups WHERE user_login = $new_username");
      foreach ($new_usermeta as $meta_key=>$meta_value) {
        update_usermeta($user_id, $meta_key, $meta_value);
      }
    }

    Any ideas? What's the more graceful way to do this? Is storing the new fields in wp_usermeta unnecessary? (Seems useful in order to get_usermeta later.)

    Note: I've looked through code for Register Plus and Cimy Extra Fields do this, but I can't figure it out -- both plugins are probably doing things much more sophisticated than what I'm trying to do.

  2. jlm99
    Member
    Posted 4 years ago #

    I think I solved this. The more appropriate hook is the action wpmu_activate_user (which hooks in wpmu_activate_signup in wpmu-functions.php), making it unnecessary to do a database query. Something like this seems to work:

    ...
    add_action('wpmu_activate_user', 'usas_reg_usermeta', 10, 3); // That last "3" is important to catch all three variables passed by hook, including $meta
    ...
    function usas_reg_usermeta($user_id, $password, $meta) {
      $value = $meta['key']; // where "key" is any given meta key
      update_usermeta($user_id, 'key', $value);
    }

About this Topic