PHP, WordPress – User information not being sent to User Profile

I have a WordPress site with a membership plug-in added. Originally, the plug-in only captured username/password to sign up. We wanted to capture more information as this is not tied to WooCommerce, unfortunately. An example php file was found and I completed it to capture what info we want from the customer (see below).

All these new fields in the registration form are required. However, the problem was when a user tried to upgrade their membership, they could not. They would receive an error message that said “address, city, state, etc. is required”. The form was gone, –since they were logged in– but the fields were still required. To get around this, I put the ending of the code in a for-loop like so:

foreach($fields as $field)
  if ( is_user_logged_in() ) {
      array("required"=>false);
}
  pmprorh_add_registration_field(
      "after_password", // location on checkout page
      $field            // PMProRH_Field object
  );

The currently problem is now, the new fields are not added to the user’s profile. I can still see the information entered by exporting an Excel spreadsheet, but the fact that WordPress does not capture this info and place it in the user’s profile is a little scary.

Is there a better way to code this so that (1.) all info get passed to the user’s profile and I can see it in the admin side of the site and (2.) the user receives no error if they try to upgrade their membership account?

Code is below. Thanks!

<?php
/*
Plugin Name: Register Helper Example
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-customizations/
Description: Register Helper Initialization Example
Version: .1
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
//we have to put everything in a function called on init, so we are sure 
//Register Helper is loaded
function my_pmprorh_init()
{
//don't break if Register Helper is not loaded
if(!function_exists("pmprorh_add_registration_field"))
{
    return false;
}

//define the fields
$fields = array();
$fields[] = new PMProRH_Field(
    "Birthday",                   // input name, will also be used as meta 

    "select",                    //type of field
    array(
      "required"=>"true",        //required and mark it as such
      "profile"=>true,
      "memberslistcsv"=>true,
      "label"=>"Birthday",
        "options"=>array(       // <option> elements for select field
                "" => "",       // blank option - cannot be selected if this 
//field is required
            "January"=>"January",     // <option 
value="January">January</option>
            "February"=>"February",  // <option 
value="February">February</option>
            "March"=>"March",  // <option value="March">March</option>
            "April"=>"April",  // <option value="April">April</option>
            "May"=>"May",  // <option value="May">May</option>
            "June"=>"June",  // <option value="June">June</option>
            "July"=>"July",  // <option value="July">July</option>
            "August"=>"August",  // <option value="August">August</option>
            "September"=>"September",  // <option 
value="September">September</option>
            "October"=>"October",  // <option 
value="February">February</option>
            "November"=>"November",  // <option 
value="November">November</option>
            "December"=>"December"  // <option 
value="December">December</option>
    )));

    $fields[] = new PMProRH_Field(
        "firstname",              // input name, will also be used as meta
        "text",                 // type of field
        array(
            "label"=>"First Name:", //displays the field's label
            "size"=>20,         // input size
            "class"=>"firstname", // custom class
            "profile"=>true,    // show in user profile
            "required"=>true,    // make this field required
            "memberslistcsv"=>true
            ));

    $fields[] = new PMProRH_Field(
        "lastname",
        "text",
        array(
            "label"=>"Last Name:",
            "size"=>20,
            "class"=>"lastname",
            "profile"=>true,
            "required"=>true,
            "memberslistcsv"=>true
            ));

    $fields[] = new PMProRH_Field(
        "address",
        "text",
        array(
            "label"=>"Address:",
            "size"=>20,
            "class"=>"address",
            "profile"=>true,
            "required"=>true,
            "memberslistcsv"=>true
            ));

    $fields[] = new PMProRH_Field(
        "address2",
        "text",
        array(
            "label"=>"Address Line 2:",
            "size"=>20,
            "class"=>"address2",
            "profile"=>true,
            "memberslistcsv"=>true
            ));

    $fields[] = new PMProRH_Field(
        "City",
        "text",
        array(
            "label"=>"City:",
            "size"=>20,
            "class"=>"city",
            "profile"=>true,
            "required"=>true,
            "memberslistcsv"=>true
            ));

      $fields[] = new PMProRH_Field(
          "State",
          "text",
          array(
              "label"=>"State:",
              "size"=>5,
              "class"=>"astate",
              "profile"=>true,
              "required"=>true,
              "memberslistcsv"=>true
              ));

    $fields[] = new PMProRH_Field(
        "Zip",
        "text",
        array(
            "label"=>"Zip Code:",
            "size"=>10,
            "class"=>"zipcode",
            "profile"=>true,
            "required"=>true,
            "memberslistcsv"=>true
            ));

/*add the fields into a new checkout_boxes area of the checkout page.
fields only appear if user is not logged in.
this is done to prevent required fields from being hidden.*/

foreach($fields as $field)
if ( is_user_logged_in() ) {
  array("required"=>false);
}
  pmprorh_add_registration_field(
      "after_password", // location on checkout page
      $field            // PMProRH_Field object
  );
}
add_action("init", "my_pmprorh_init");
?>

Read more here: PHP, WordPress – User information not being sent to User Profile

Leave a Reply

Your email address will not be published. Required fields are marked *