Dev Notes

Various Cheat Sheets and Resources by David Egan/Carawebs.

Update WordPress User Meta


WordPress
David Egan

#Build an Array The objective is to create a two-dimensional array that holds information about user progress - in this case, when a user finishes a workbook, information is stored in a ‘completed_workbooks’ field.

Update User Meta Function

/**
*
* Update usermeta when a student completes a workbook.
*
* @param int $student_ID Student user ID
* @param int $workbook_ID Workbook(CPT) post ID
* @param int $stage Student's current stage
* @param int $workbook_total_stages Number of stages
* @param string $supervisor_name
* @param string $coord_name
* @param string $company_name
*
*
*/
function carawebs_finished_workbook (
  $student_ID,
  $workbook_ID,
  $stage,
  $workbook_total_stages,
  $supervisor_name,
  $coord_name,
  $company_name
  ){

  if ( $stage === $workbook_total_stages + 1 ) {

    // The student has completed the current workbook
    // -------------------------------------------------------------------------
    $date = date("F j, Y, g:i a");

    $existing_completed_meta = get_user_meta( $student_ID, 'completed_workbooks', true );

    if ( $existing_completed_meta ){

      // Previous workbooks have been completed
      // -----------------------------------------------------------------------

      $new_data = array (
          'workbook_ID'       => $workbook_ID,
          'supervisor_name'   => $supervisor_name,
          'coordinator_name'  => $coord_name,
          'company_name'      => $company_name,
          'date_completed'    => $date
      );

      $existing_completed_meta["workbook_ID_$workbook_ID"] = $new_data;

      update_user_meta( $student_ID, 'completed_workbooks', $existing_completed_meta );


    } else {

      // First workbook completed - add 'completed_workbooks' array
      // -----------------------------------------------------------------------

      $completed_meta = array(
        "workbook_ID_$workbook_ID"  => array (
          'workbook_ID'       => $workbook_ID,
          'supervisor_name'   => $supervisor_name,
          'coordinator_name'  => $coord_name,
          'company_name'      => $company_name,
          'date_completed'    => $date
        )
      );

      update_user_meta( $student_ID, 'completed_workbooks', $completed_meta );

    }

  }

}

The Resulting Array

The resulting array looks like this:

array(2) {
  ["workbook_ID_14"]=>
  array(6) {
    ["workbook_ID"]=>
    string(2) "14"
    ["supervisor_name"]=>
    string(14) "Clark Griswold"
    ["coordinator_name"]=>
    string(10) "David Egan"
    ["company_name"]=>
    string(8) "ACME Ltd"
    ["date_completed"]=>
    string(25) "February 8, 2015, 3:50 pm"
  }
  ["workbook_ID_13"]=>
  array(6) {
    ["workbook_ID"]=>
    string(2) "13"
    ["supervisor_name"]=>
    string(14) "Clark Griswold"
    ["coordinator_name"]=>
    string(10) "David Egan"
    ["company_name"]=>
    string(8) "ACME Ltd"
    ["date_completed"]=>
    string(25) "February 8, 2015, 3:51 pm"
  }
}

Retrieve Data

To retrieve the data:

$completed_workbooks = get_user_meta( $student_ID, 'completed_workbooks', true );

// Access data for each workbook:

  $supervisor_name  = $completed_workbooks["workbook_ID_$workbook_ID"]['supervisor_name'];

comments powered by Disqus