Skip Navigation

[Resolved] Create a form that admin can submit to be viewed only by a user.

This thread is resolved. Here is a description of the problem and solution.

Problem:

I need admins to be able to write notes to users, which are viewed by the user only.

Example: Student takes a test, instructor grades test. Instructor submits online form detailing the results of the test. Only the student can view their own results.

Solution:

You can try to setup the student user into note post author field, for example:

https://toolset.com/forums/topic/create-a-form-that-admin-can-submit-to-be-viewed-only-by-a-user/#post-1128095

Relevant Documentation:

This support ticket is created 5 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 5 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#1127753

Tell us what you are trying to do?
I need admins to be able to write notes to users, which are viewed by the user only.

Is there any documentation that you are following?
I haven't found any.

Is there a similar example that we can see?
Example: Student takes a test, instructor grades test. Instructor submits online form detailing the results of the test. Only the student can view their own results.

I imagine Relationships and Views would be able to handle this, but I do not see the ability to assign a relationship between a custom post type and a user.

What is the link to your site?
hidden link

#1128095

Hello,

You can try to setup the student user into note post author field, for example:

1) Create a custom post type "notes",

2) Create a numeric post field "student-id", register it to post type "notes"

3) Create a custom user role "student"

4) Create a user view, query student user, output the student name and student ID as JSON format, use custom codes to remove all other HTML tags, here is the detail steps:

a) create a shortcode to hide the extra DIV tag from Views ouptut, add below codes into your theme/functions.php:

// remove extra HTML tags
add_shortcode('hide-it', 'hide_it_func');
function hide_it_func(){
    return;
}

b) Create a user view "student-ids",query all student user, in views loop, output the student name and student ID as JSON format, and hide other HTMl tags:

[hide-it][wpv-layout-start][/hide-it]
[wpv-items-found]
    <!-- wpv-loop-start -->
    <wpv-loop>
      [wpv-item index=1]{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}
      [wpv-item index=other],{"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}
    </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
    [wpv-no-items-found]
        <strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
    [/wpv-no-items-found]
[hide-it][wpv-layout-end][/hide-it]

4) Create a post form for creating "notes" post, in the form content, display a general field, and use above view's shortcode to populate the field options, for example:

[cred_generic_field field='student-id' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="student-ids"] ]
}
[/cred_generic_field]

https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field

5) Display above form in front-end, let teach users to create new "note" post, after submit this form, use below codes to update the post author as selected student

//update post author
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
	// if a specific form - Form 
	if ($form_data['id']==12345) {
		update_post_meta($post_id, 'wpcf-student-id', $_POST['student-id']);
	}
}

Please replace 12345 with the post form ID of step 4)

6) In a single "note" post, you can get the "student-id" field value, use it to compare with current logged-in user's ID, then display the note information, for example:

[wpv-conditional if="'[types field='student-id' output='raw'][/types]' eq '[wpv-current-user info="id"]'"]
Display some information
[/wpv-conditional]

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

#1128512

Great solution, thank you!! I'd love to see user/post relations integrated into a future update!

For others reading this, the solution seems to assign uses the "student-id" field and assigns it to a single user upon the submission of the "note" form. Step 6 then hides any posts that do not belong to the assigned student-id. This allows the instructor to remain the author, while still restricting the content to be viewable only by the user with the correct student-id.

Follow the instructions exactly, and you can have user notes as well!

#1128776

You are welcome.

In the latest version of Views plugin, when you edit a view, there is an option under the section "Loop Editor":
"Disable the wrapping DIV around the View"

It can also remove those extra HTML DIV tags without custom shortcodes.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.