Support

Account

Home Forums General Issues Recoding a User Field to a new one

Solving

Recoding a User Field to a new one

  • I have a user field from a membership plugin that classifies users as having certain membership levels from 1 to 7. I’d like to have a new field created that takes each of those numbers and translates them to a label, for example 1 = Student and then display them in the User view. Any way to do this via advanced custom fields?

  • Anything is possible, but it may not be something you’d do with ACF.

    Do you mean display the field or do you mean allow the value to be edited. I would use ACF if I wanted a different way to edit this value but if I wanted to just display this “translation” then I would look to WP documentation on how to edit the content of the page you want to change.

  • I was thinking this may be beyond ACF. I’d like something to edit the field and have that updated value be in a second one.

  • If you want another field, yes this is possible.

    Basically you would create the new field. Then you would create an acf/save_post filter that will get the value from your new field and update the correct value for the other plugin. The exact details of doing this are highly dependent on the other plugin https://www.advancedcustomfields.com/resources/acf-save_post/

    You could use a select field for this, or a radio field.

    A basic example

    
    add_filter('acf/save_post', 'my_acf_update_user_field', 20, 1);
    function my_acf_update_user_field($post_id) {
      if (substr($post_id, 0, 5) != 'user_') {
        // not a user being updated
        return;
      }
      // get user ID from $post_id
      $user_id = intval(substr($post_id, 5));
      // get value of your field
      $value = get_field('your-field-name', $post_id);
      // do whatever needs to be done to update the field for other plugin
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.