Skip Navigation

[Resolved] Show custom fields in cred and in frontend based on roles

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1213891

Everything in this site happens in frontend, so CRED, post editing forms and Access are involved.

In the site basic user - let's call him Adam - who has Subscriber role, can create and edit his own posts. And there is Eve, who is not logged in site visitor, she can see Adam's posts.

Now I would like to add some extra fields for Adam to fill if he has another role, Customer. So 3 things should happen:

1. Adam can fill those extra fields when he has the Customer role.
2. Eve can see those extra fields too.
3. If Adam's role changes back to Subcriber, the fields should not be visible for both Adam or Eve.

Is this possible? Could the extra field content be emptied/deleted or hidden from both Adam and Eve?

#1213937

Hi, in simple cases you can use Conditional Groups in Forms to show or hide specific input fields based on User Role. Then on the front-end when you are displaying that post, you can use Conditional HTML to show or hide fields based on User Role. I would need to investigate more complex cases that involve required fields or data-dependent display, so let me know if you need more details about those.

Step 3 is more difficult, because you must be able to detect when a User's role is changed. In Toolset, there is no such event hook. In WordPress there is an action set_user_role. You can use that action to trigger any arbitrary code. Inside that trigger you would have to query all of Adam's posts, then loop over all those posts and empty the field values using delete_post_meta. Here's an example:

add_action( 'set_user_role', function( $user_id, $role, $old_roles ) 
{
  $query_args = array(
    'post_type'   => 'post-type-slug',
    'post_status' => 'published',
    'fields'      => 'ids',
    'author'      => 12345
  );
  $query = new WP_Query( $query_args );
  if ( $query->have_posts() ) {
    foreach ( $query->posts as $post_id ) {
      delete_post_meta( $post_id, 'wpcf-field-slug' );
    }
  }
}, 10, 3 );

You would replace post-type-slug with the slug of the post type containing the custom field. You would replace 12345 with the numeric ID of the post author, or a variable that represents that value. Then you would change wpcf-field-slug to match the slug of your custom field. You should use the wpcf- prefix here, so if the slug in wp-admin is "some-field" then you would replace wpcf-field-slug with wpcf-some-field.
https://developer.wordpress.org/reference/hooks/set_user_role/

#1214062

Hi Christian and thank you,

I have the point 1 covered with this I think:

[wpv-conditional if="( '[wpv-current-user info="role"]' eq 'customer' )"]
[cred_field scaffold_field_id='customerfield' field='customerfield' force_type='field']
[/wpv-conditional]

About point 2: is it possible to show the extra customer fields in frontend only when the author of the post belongs to role Customer? If this was possible then I wouldn't need to delete the fields completely (as you describe in point three, I think).

#1214076

Ok, I found a solution to the points 2-3: just show or hide (not delete/empty) the extra fields from both visitors and also from the cred form based on author's role. This thread helped:
https://toolset.com/forums/topic/conditional-display-based-on-user-role-of-post-author/

Thank you Christian again to point me to right direction!