Skip Navigation

[Resolved] Allow edit custom post specific taxonomy

This support ticket is created 5 years, 3 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Author
Posts
#1342705

Tell us what you are trying to do?
Like, this one : https://toolset.com/forums/topic/create-a-user-role-with-only-access-to-a-specific-taxonomy-inside-a-post/

I want to restrict the edition of custom posts depending on one taxonomy (which is category).
I have multiple crises on my website, each crise has multiple case studies.
I want to create a role which will be able to edit and publish multiple case studies, coming from juste one crise.
(If I have to create a role for each crises it's alright for me).

Is it possible ? With Post Group, it's just Read Access settings, and not edit.

What is the link to your site?
hidden link

#1343649

Hi there,

Thank you for waiting.

I've performed some tests and here are my suggestions around what you're planning to achieve.

To control case study post's addition and edit permission, with respect to taxonomy terms ( crises ), I'll suggest using a new user field. I do not suggest adding a new user role for each crises because it will become complicated to manage in the longer run. If you'll need to add a single change to case study author permissions/privileges, it will need to be applied across all user roles.

1. You can add just one custom user role for example "Case Study Authors", and all users who'll be allowed to add/edit case study posts will be added with this user role.

2. To segregate these users based on the allowed taxonomy terms ( crises ), you can add a new select type user field "Allowed Category", which will hold the term names as the option titles and term slugs as the option values.
( ref: https://toolset.com/documentation/user-guides/user-fields/ )

It is very important that the option values and the term slugs match because these will be used later for the term assignment and conditional comparison.

Example screenshot of the user field with options:
hidden link

Example screenshot of taxonomy terms:
hidden link

3. Whenever you'll add a new case study author, you'll assign the user role "Case Study Authors" and you'll see the new user field "Allowed Category" on his/her profile edit screen in the admin area. Through that, you can select the crises term this user is allowed to add/edit.

Note: You can use the Toolset access plugin to restrict access to this new user field, only to administrator role, so that authors can't change the allowed crises term, on their own.
https://toolset.com/documentation/user-guides/access-control-for-user-fields/

4. Next, please create a new post form, which will allow your authors to add a new case study post from the front-end, but make sure it doesn't include the field to attach crises term.
https://toolset.com/documentation/getting-started-with-toolset/publish-content-from-the-front-end/forms-for-creating-content/

5. The crises term will be programmatically assigned using the "cred_submit_complete" hook.
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_complete )

For example, you can add the following function in your active theme's "functions.php" file:


add_action('cred_submit_complete', 'add_hidden_tax_function',10,2);
function add_hidden_tax_function($post_id, $form_data)
{
	// if a specific form
	if ($form_data['id']==123)
	{
		$user_term = types_render_usermeta( "allowed-category", array( "user_current" => true ) );

		if(!empty($user_term)) {
			wp_set_object_terms( $post_id, $user_term, 'case-study-category' );
		}
	}
}

The above function will execute when the author will submit a new post through this front-end form. It will get the value of slug saved in the currently logged-in user's field and then assign it as a taxonomy term, to this newly created post.

Please replace "123" with the actual ID of your new post form (step 4), "allowed-category" with the actual slug of the user field (step 2) and the "case-study-category" with the slug of the taxonomy that you're using for this segregation.
( for example, if you're using default post category taxonomy with case study posts, its slug is "category" ).

6. You'll also need a new post edit form, so that the case study authors can edit the existing case studies.
https://toolset.com/documentation/getting-started-with-toolset/publish-content-from-the-front-end/forms-for-editing/

Again, it is important, that this post edit form, also doesn't have the field to edit the crises term.

7. To make sure that only allowed case study authors can use this edit form, you can wrap the shortcode for the edit form inside a conditional block, that checks whether the current post's crises term and the currently logged-in user's field value matches or not:

The "wpv-post-taxonomy" shortcode will return the slug of the taxonomy term attached to the current post:
( ref: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy )


[wpv-post-taxonomy type="case-study-category" format="slug"]

Note: Replace "case-study-category" with the correct taxonomy.

The Types fields API can be used for the value of currently logged-in user's custom field value:
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/#select )


[types usermeta='allowed-category' current_user='true' output="raw"][/types]

Note: Replace "allowed-category" with the actual user field slug.

These shortcodes can be used in the conditional code blocks like this:
( ref: https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/ )


[wpv-conditional if="( '[wpv-post-taxonomy type="case-study-category" format="slug"]' eq '[types usermeta='allowed-category' current_user='true' output="raw"][/types]' )"] 
Form to edit post!
[/wpv-conditional]


[wpv-conditional if="( '[wpv-post-taxonomy type="case-study-category" format="slug"]' ne '[types usermeta='allowed-category' current_user='true' output="raw"][/types]' )"] 
You're not allowed to edit this post!
[/wpv-conditional]

Important note: This whole setup depends on the fact that that your case study authors are only allowed to add/edit case study posts, through front-end Toolset Forms. They'll still be able to add/edit posts through the backend admin area, which is why it is important that these users are not allowed to access the admin area, at all.
( ref: hidden link )

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1413407

Hi !

I was wondering what you mean by " make sure it doesn't include the field to attach crises term " ?
Is it the user field or the taxonomy field of the custom post ?

Regards,