chronosphere
Forum Replies Created
-
I have improved my code by no longer relying on hard-coded values. Now, the code automatically finds the relevant fields and saves them from deletion:
// get all ultimate member forms $query = get_posts( array( 'numberposts' => -1, 'post_type' => 'um_form' )); // find all the dropdowns (select), checkboxes, and radio fields in fields where the visibility is view-only and the access role is the current user's $persist_data = array(); foreach ($query as $q) { $meta = get_post_meta($q->ID, '_um_custom_fields'); foreach ($meta[0] as $m) { if ( ($m['type'] == 'select' || $m['type'] == 'checkbox' || $m['type'] == 'radio') && $m['visibility'] == 'view' && $m['roles'][0] == wp_get_current_user()->roles[0]) { array_push($persist_data, $m['metakey']); } } } // remove duplicate field metakeys $persist_fields = array_unique($persist_data); // filter the submitted data before the database fields are updated add_filter( 'um_user_pre_updating_profile_array', function( $to_update, $user_id ) use ($persist_fields) { return persist_content($to_update, $user_id, $persist_fields); }, 10, 2 ); // loop through the metakeys in $persist_fields and make the $to_update equal to the previous value function persist_content( $to_update, $user_id, $persist_fields) { um_fetch_user( $user_id ); for ($x = 0; $x < count($persist_fields); $x++) { $to_update[$persist_fields[$x]] = um_user($persist_fields[$x]); } return $to_update; } // reset the focused user um_reset_user();I was able to resolve my problem using the code I gave in the last step.
How can I figure out what the structure of $arr is? In the past, I’ve just had to guess that certain filter hook variables were nested arrays, but that was sheer luck. Is there documentation on how $arr is structured?
Is there a hook of some sort that can access field properties such as the “Can user edit this field” property? I essentially need someone to point me in the right direction regarding code I will be writing.
I am trying to validate a custom field, yes.
So that others can hopefully solve this problem for their own purposes, I will post my final code solution that fixes the issue of fields getting overwritten by blank data when the visibility is set to View Mode Only:
// Since my forms are only seen by certain roles, the fields that are set to View Only Mode are different if ( um_user( 'user_role' ) == 'hr') { // add your own field metakeys to this array $persist_fields = array('field1', 'field2', 'field3'); } if ( um_user( 'user_role' ) == 'employee' ) { $persist_fields = array('field7'); } // passing anonymous function (closure) to access the $persist_fields array add_filter( 'um_user_pre_updating_profile_array', function( $to_update, $user_id ) use ($persist_fields) { return persist_content($to_update, $user_id, $persist_fields); }, 10, 2 ); function persist_content( $to_update, $user_id, $persist_fields) { // since different roles are viewing/editing other's profiles, load that user's profile um_fetch_user( $user_id ); // loop through $persist_fields array for ($x = 0; $x < count($persist_fields); $x++) { $to_update[$persist_fields[$x]] = um_user($persist_fields[$x]); } return $to_update; } um_reset_user();Ok, I finally fixed that issue by making sure the code only runs when the HR user role is logged in. Now my only question is whether I can programmatically get all the fields in a form and effectively filter only a certain type of field with certain properties (e.g. dropdown, current visibility, etc.). The documentation is unfortunately very spartan and barely gives enough information to know what each filter and action hook actually does.
Awesome! That one change worked wonders!
Now I just need to know what PHP variable, method, or function returns the array of the user’s submitted changes, because right now, all changes are thrown away once pressing submit, which is obviously still a big problem.
Also, try turning up the pressure on the developers on that bugfix request, because it would be great if this wouldn’t need any workaround. In any case, thanks for your assistance!
No, instead I am making it so there are multiple profile forms, one for each access level. The ‘Employee’ user role can only see and edit certain fields, but there are two other forms for the ‘HR’ and ‘Manager’ roles. These are the three profile forms per Employee:
Employee – Employee Access
Employee – HR Access
Employee – Manager Access
Each form has different visibility and edit settings that only affect who is accessing the data. The profile page has all three forms on it, but I use block visibility to hide the irrelevant forms based on who is logged in.My problem is that the HR role is supposed to only see certain fields but not edit them, but checkboxes, dropdowns, and multiselect fields whose visibility is set to “View mode only” lose data when that form is submitted.
I have seen the workaround on another forum post, but when trying it, it doesn’t work for me. I assume this is because the logged in user is different from the user being accessed. When I tried to edit the code to reflect this, it actually throws away every change I make when submitting the form and still throws away data from view only checkboxes, etc.
Here’s my code so far:
add_filter( 'um_user_pre_updating_profile_array', 'persist_content', 10, 2 ); function persist_content( $to_update, $user_id ) { um_fetch_user( um_profile_id() ); $to_update['tasks'] = um_user( 'tasks' ); $to_update['training'] = um_user( 'training' ); um_reset_user(); return $to_update; }- This reply was modified 3 years, 10 months ago by chronosphere.
- This reply was modified 3 years, 10 months ago by chronosphere.