Support

Account

Home Forums General Issues Allow roles to be set to certain users

Solved

Allow roles to be set to certain users

  • I know it’s possible to set roles to be for certain user roles like editor or subscriber. But is it possible to set it to a certain user?

    I was keen to make a custom field but only allow as an example Jeff to be allowed to add content to the field but not Tony but they both have to be subscribers

  • Hi @planktonwebdesign

    yes, this is possible, however it is not part of the core plugin.
    You will need to create a new location rule which will take about 10 mins to do.

    With this new location rule, you can look at the current logged in user and compare the username to ‘jeff’. If a match is found, return true, otherwise, return false.

    http://www.advancedcustomfields.com/resources/tutorials/custom-location-rules/

    Thanks
    E

  • Thanks Elliot,

    Does this mean everytime I add a new user though and I want them to access a custom field all of their own I’ll need to create a new location rule or with the above mentioned tutorial will it enable me to match current logged in user to anyone from the user list?

    Sorry to take up your time. I’m making a rostering page that employees can login and accept decline jobs but want them to yes/no only their custom field and not have access to everyones. Is this method I’m mentioning not the best. If using the front end edit plugin I know I could hide the custom fields with jquery and only show the logged in user by matching the username with the class that is applied to the wrapper of the radio buttons.

    Mike

  • Hi @planktonwebdesign

    Thanks for the extra info. Knowing this, I would not create a custom location rules. What you should do is create your field group with a field for each user. You need to have some sort of patern in the field name which has either the user’s ID or the user’s username in it. So perhaps “accept_job_dave” or “accept_job_3”.

    Then, use the filter acf/load_field (http://www.advancedcustomfields.com/resources/filters/acfload_field/) to remove fields.

    Lets say the current logged in user is “jeff”. In your filter, you would target all fields, or perhaps only all radio fields (explained in docs).

    Then, your code would look at the field name ($field[‘name’]). If this name included the string “accept_job_”, then you would compare the currentl logged in user to the field like so:

    
    <?php 
    
    if( strpos($field['name'], 'accept_job_') !== false )
    {
    	// accept_job_ was found in field name
    	$user = str_replace('accept_job_', '', $field['name']);
    	
    	
    	// $user is no 'jeff', find curent logged in user
    	$current_user_name = wp_function_to_find_current_username();
    	
    	
    	// compare and completely remove field if this user is not meant to see it!
    	if( $current_user_name != $user )
    	{
    		$field = null;
    	}
    	
    	
    	return $field;
    }
    
     ?>
    

    I hope that code example helps explain what you need to do.

    Thanks
    E

  • Cool thanks Elliot, so do I just drop this bit of code into my functions.php file in my theme? Do I need to write any of the code from the acf/load_field filter resources link?

    Is there a tweak to the code you have above that can still make the fields visible/editable by admin. So:

    
    // compare and completely remove field if this user is not meant to see it! Unless they are admin, then always show it
    	if( $current_user_name != $user {but do show if the user is admin} )
    	{
    		$field = null;
    	}
    	
    	
    	return $field;
    }
    

    Really appreciate your help!!!

  • Hi @planktonwebdesign

    Do I need to write any of the code from the acf/load_field filter resources link?
    YES

    The code I supplied was a concept and would not work or do anything bu crash your site it you placed it into your functions.php file.

    Please read the docs I provided and read over my previous comment to understand the task.

    This is pretty advanced stuff. If your not at this level, consider hiring some help on this one.

    Thanks
    E

  • Ok cool thanks Elliot, I’ve some more examples of the acf/load_field filter in action so will figure it out.

    Thanks for your help answering my question really appreciate the time,

    Mike

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Allow roles to be set to certain users’ is closed to new replies.