Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * @param int $user id
- * @param string $meta_key
- * @param string $new_value - the new value to be added to the array
- */
- function meta_update($user_id, $meta_key, $new_value) {
- // Get the existing meta for 'meta_key'
- $meta = get_user_meta($user_id, $meta_key, true);
- // Do some defensive coding - if it's not an array, set it up
- if ( ! is_array($meta) ) {
- $meta = array();
- }
- // Push a new value onto the array
- $meta[] = $new_value;
- // Write the user meta record with the new value in it
- update_user_meta($user_id, $meta_key, $meta);
- }
- /*
- * @param int $user_id
- * @param int $certification_id - certifications post_id
- * @param string $name - TODO: auto pull post title
- * @param string $renewal_date - user provided date
- */
- function cpe_enroll_user( $user_id, $certification_id, $name, $renewal_date ) {
- // $meta = get_user_meta( $user_id, 'certifications', false)[0];
- $meta = get_user_meta( $user_id, 'certifications', true);
- if ( !array($meta) ) {
- $meta = array();
- }
- $enrollment_data = array(array(
- 'certification_id' => $certification_id,
- 'certification_name' => $name,
- 'cpes_required' => $cpes_required,
- 'renewal_date' => $renewal_date,
- ),
- );
- if(!empty($meta)) {
- // foreach($meta as $key => $value) {
- if(!in_array($enrollment_data[0]['certification_id'], array_column($meta, 'certification_id'))) {
- $meta_update = array_merge($meta, $enrollment_data);
- update_user_meta( $user_id, 'certifications', $meta_update);
- }
- // }
- } else {
- update_user_meta( $user_id, 'certifications', $enrollment_data);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement