Skip Navigation

[Resolved] Link University admins and users

This support ticket is created 3 years 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)

This topic contains 11 replies, has 2 voices.

Last updated by salimA 2 years, 12 months ago.

Assisted by: Waqar.

Author
Posts
#2019137

Good morning

I have a user form that allows users to register on our service. the user should select one university to study with

we have also admin page for the university admin to accept and reject the applications

I have created two pages one for admin 1 and the other for admin 2. each page displays the applicants who select the same university

So, I need to create a different page and different views and different login permission for each university admin. which is difficult if we have a lot universities

I would like to have only one page with one view. the view content should changed based on the logged-in user.

#2019571

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Reviewing what we have discussed in the chat, another alternative can be to add a new "select" type user field (let's call it "Connected University") which will be available to both University admins and as well as regular users.

Note: You don't need to include any options for this field, as they will be populated dynamically using the "wpt_field_options" filter:
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options

To dynamically generate the University options for this select field, you'll create a new custom post type "Universities" and add all Universities as individual posts in this post type.

Next, you'll need a custom function attached to the "wpt_field_options" filter that will get the list of all available posts from the "Universities" post type and use them as options for this new "Connected University" user field.
( post titles will be used as the option title and post IDs will be used as option value )

Example:


add_filter( 'wpt_field_options', 'populate_university_field_options', 10, 3);
function populate_university_field_options( $options, $title, $type ){
    switch( $title ){
        case 'Connected University':
            $options = array();
            $args = array(
                'post_type'        => 'university',
                'posts_per_page'   => -1,
                'post_status'      => 'publish',
            );
            $posts_array = get_posts( $args );
            $options[] = array('#value' => '', '#title' => '---',);
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => $post->post_title,
                );
            }
        break;
    }
    return $options;
}

Note: Please replace "Connected University" with your actual user field's title and "university" with your actual "Universities" post type slug.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Once the admin and regular users will have the correct university stored in the user field, you'll be able to use conditional display, so that only connected university's information can be accessed by the respective admins and users.

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2020533

Thank you Waqar for your suggestions, I will try it and I will come back to you if I need

#2020557

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You're very welcome and please let me know if you have any follow-up questions.

For a new question or concern, please start a new ticket.

#2021613
user registration form.png
user field.JPG
snippet.JPG
Post type.JPG

Hi Waqar,

I did what you suggest, But the select field displayed without options in the user form. Please check the attached screenshots.

#2021717

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

From the screenshots, I see that the title of the field is "Organization" and the slug is "org".

And, at line# 13 in the snippet, the field's slug and not the title is used. Can you please change the "org" in the snippet to "Organization" and then check the field again?

#2021741

Thank you for your quick reply. It displayed now.

So now, how to create only one page with one view. the view content should be changed based on the logged-in user (Admin).
If the admin of org1 login to the page, the view displays all requests that select org1 and vice versa.

#2023239

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You can use the query filter setting in the view for achieving this.

For example, suppose you have to show a list of all users where the organization/university is the same as the current logged-in admin.

You'll create a user view and set it to show the users from the user role that you're using for university users. In the query filter settings, you can add a filter for the "Organization" field, to show only those users where the value of this field is equal to the value passed through a shortcode attribute "org".
( screenshot: hidden link )

And you can get the currently logged-in user's "Organization" field value, using the shortcode:
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/#select )


[types usermeta='org' output='raw' current_user='true'][/types]

And you can pass this field's value inside the view's shortcode like this:


[wpv-view name="view-slug" org="[types usermeta='org' output='raw' current_user='true'][/types]"]

Note: Please replace "view-slug" with the actual slug of your view.

As a result, when the view's output will load, it will show only those users, where the selected organization is the same as the current logged-in admin.

Likewise, you can use similar filtering for other cases too.

#2027111
4.JPG
3.JPG
2.JPG
1.JPG

Hi Waqar,

I have tried as you mention to filter the users based on the currently logged-in user. But the view shows ( no item found).
when I checked the user's profile from the backend for both user and admin, the Organization field shows (not set) as on the attached image (1).
But in the front end profile, it is showing the organization based on the selected value from the post type. as attached (2)

#2027353

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

Can you please share temporary admin login details, along with the example page where this view can be seen?

To view the options added through the custom code in the admin area user edit screen, you'll need to uncheck the option "Required" from that "Organization" field's settings.

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

#2032723

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

I noticed that in the view "test view" the query filter was set to check for the "Organization" field, with a constant value "1".
( screenshot: hidden link )

I assigned each available university to each subscriber user and removed the query filter and they both started showing.
( screenshot: hidden link )

Next, I added a query filter, for the "Organization" field so that it matches the value passed through the shortcode attribute "org".
( screenshot: hidden link )

As a result, only the user whose "Organization" field value was the same as the currently logged-in user started showing.
( screenshot: hidden link )

I hope these screenshots will make it more clear.

#2033787

Many Thanks Waqar. It works now as I want.

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