Skip Navigation

[Resolved] Toolset Display Memberpress Membership Name

This support ticket is created 5 years, 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 11 replies, has 2 voices.

Last updated by orlaF 5 years, 7 months ago.

Assisted by: Nigel.

Author
Posts
#1246348

Is it possible to display a Memberpress Membership Name in a post view?

I have a post type "licensed-members" that only paid members can create. I would like to automatically display the membership name (their license type) on that post type but I have not been able to figure out how to show it. I've tried the hidden custom fields '_mepr_auto_gen_title' & '_mepr_plan_code' but neither show any output when I use them in the post view.

I figure that there is probably some issue with the membership name being tied to the user but I am really struggling to figure it out so any help or pointers would really be appreciated. Ideally I would also like to be able to filter the users by the License Type also on this page (Instead of Choose Topic - it would be a Choose License Type dropdown): hidden link

Kind Regards,
Orla

#1246445

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Orla

I'm not familiar enough with MemberPress to know how it stores its data, and to be able to help you I need to know that.

If I understand this set-up correctly, you use MemberPress to register users. These users can create a licensed coach post (this is effectively a profile post about them).

The users are the authors of these licensed coach posts or connected in some other way?

And what you want is when displaying a licensed coach post to output the membership level of the user that "owns" that post, is that right?

I'm guessing that would be stored as user meta, but you'll need to confirm how exactly, it should be something described in the documentation for MemberPress.

Where you say "I would also like to be able to filter the users by the License Type" this licence type is what? The same as the membership level we've already been discussing? Because if that's the case the problem is that this View doesn't display users (which is where that data would be stored), it displays licensed coach posts, and if you want to be able to filter licensed coach posts by this field, you would need to store that field against the licensed coach posts. So it may be that when you create the licensed coach post you record what the membership level is as a field on the licensed coach posts.

But this rather depends on where this data originates.

#1247341

Hi Nigel,

Sorry for the delay with replying to this.

I've now assigned each membership a User Role that corresponds with the licence name. This is the WordPress Additional Capabilities "Other Roles".

Is there any way to show these "Other Roles" on a post view?

I have found a hidden custom field called "_mepruserroles_roles" but haven't been able to get it to work.

Kind Regards,
Orla

#1247793

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Where is this _mepruserroles_roles stored? Is it user meta or post meta?

You can only filter something by its own properties, so you can't filter posts by user meta.

As I mentioned above, you would need to create a custom field on your posts which stored the licence name (or user role) of the person the post refers to (easiest, perhaps, if they are the author of the post).

How are you creating the posts? In the back end or using a front-end Form?

If you are creating them using a front-end Form then it should be fairly straightforward to automatically populate such a field with the role of the user making the submission, for example. I could help with that, but need to know that's what is required.

#1251229

Sorry about the delay again - I am jumping between projects at the moment, things are a little hectic.

I have been able to get Memberpress to set a user role for new members according to the membership they are assigned to. It would seem that this is what _mepruserroles_roles is but I am not 100% sure. Each member still has their primary WordPress role eg: 'subscriber', then memberpress assigns Other Roles (See screenshot: hidden link) I just don't know how to display these other user roles on the front end.

I would have thought that _mepruserroles_roles would be stored in User Meta but it shows up under non-toolset custom fields in Post Views. The following shortcode doesn't seem to do anything [wpv-post-field name='_mepruserroles_roles']

I can show the main user role by using [wpv-current-user info="role"] but it is the memberpress user role that I need access to.

Posts are being created by a front end form after they have signed up - it is an 'edit my profile' form in the members area. Their user role is set depending on their membership.

It feels like I am going around and around in circles with this and just can't figure it out. I've tried looking through the database but haven't found the other roles. I've also sent Memberpress an email on this so hopefully their reply will through a little more light on it!

#1251513

Ok, Memberpress are not willing to give any support on this.

I've found the capabilities in the database - the other roles are stored in User Meta:
[wp_12kf1_capabilities] => Array ( [0] => a:4:{s:11:"wacn-editor";b:1;s:9:"win-coach";b:1;s:8:"win-lite";b:1;s:7:"win-pro";b:1;} )

I can access the primary user role 'wacn-editor' in the Post Views using Basic Data > Current User Information > User Role but I can't seem to show or access the other roles. Ideally if I could create a conditional output tag something like this:

if user role = "win-coach" print WIN-COACH

it could work. Is this something that is possible?

#1251537

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

That's stored as a serialized array, which looks like this when unserialized:

Array
(
    [wacn-editor] => 1
    [win-coach] => 1
    [win-lite] => 1
    [win-pro] => 1
)

This doesn't seem to make much sense inasmuch as if it is stored as user meta and so belongs to a single user it means, what?, that the user has each of these membership levels?

I'm looking into this now to see if I can spot how the data is stored and am browsing their documentation.

I'll update again here soon.

#1251549

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, I'm not getting very far with setting up a demo site, I think it would help if I could look at your site and inspect the database to see what's going on.

If that's okay then please provide credentials in the next private reply.

You may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to make any changes other than to temporarily add a plugin for me to browse the database.

Can you give me an example of a user that I can use to investigate. What membership do they have, what is the custom role assigned to them.

#1251621

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Orla

If you look at Toolset > Settings > Custom Code you will see I added a code snippet to register a shortcode to output the current role of a user whose id you pass as an attribute (defaults to the current user if omitted). You have to provide a whitelist of the roles you want to output (otherwise it would output standard roles such as subscriber).

This is the code for reference:

add_shortcode('member-role', function ($atts = []) {

    // provide defaults
    $atts = shortcode_atts(
        array(
            'id' => get_current_user_id(),
        ),
        $atts
    );
    $output = '';

    // Create a whitelist of roles
    $target_roles = array( 'win-lite', 'win-coach', 'win-pro' );

    if ( $atts['id'] ){

        $user = get_user_by( 'ID', $atts['id']);
        $member_roles = array_intersect( $user->roles, $target_roles );

        $output = implode(",", $member_roles);
    }

    return $output;
});

You can see it tested on a page "Toolset", where I pass the ID 6 for your test user.

I'll leave it to you to decide the best way to use it as you know what you are aiming for, but if you get stuck let me know.

#1251677

Hi Nigel,

That worked brilliantly - I used the following nested shortcode to get the ID of the post author:

[member-role id="[wpv-post-author format='meta' meta='ID']"]

Thank you :-)!

Is it possible to filter using these roles - ideally I would love a dropdown menu the same as location that shows the whitelisted roles (Screenshot: hidden link) but am open to any solution.

Also, can I use this shortcode to conditionally show a list of users who match that role so I could create a link or page like hidden link ?

Thank you again Nigel - this has been quite a battle for me - your help has been invaluable 🙂

Kind Regards,
Orla

#1251693

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Orla

Let me repeat that you can only filter something by its own properties*.

You can line toddlers up in the playground according to their height, but not by their parents' income, because they don't know it (guessing from their clothes doesn't count). But you could if they came with the figure written across their forehead.

So that's what you need to do.

If you are filtering posts then the membership level needs to be stored as a property of posts, e.g. in some custom field. You can then filter by that.

So if the users publish the posts via front-end Forms use the cred_save_data hook to trigger code when the form is submitted that gets the (non-standard) role of the current user and saves that as some custom field. (Note that if you create a Types custom field for this it is stored in wp_postmeta with a 'wpcf-' prefix, e.g. a 'priority' field is stored with a key of 'wpcf-priority'.) There should be enough in the sample shortcode I shared for you to do that.

(See https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data)

So your posts will have the membership level available on the posts as a custom field you can filter a View by.

The next question was about displaying all users with some particular role.

That should be straightforward, just create a new User View and in the first section specify the role (custom roles should appear here as well as the standard roles). Without any other filters that View should return all users with the given role.

(* This is how WordPress queries work. You can use custom SQL queries with table joins to filter by properties of related content.)

#1251717

Showing the role was the most urgent part of this so I am incredibly grateful for your help with this Nigel.

I'll put aside some time next week to see if I can figure out a solution to filter by role type as it is not assigned on form submission but on membership signup. There has to be some way around it though :-).

Thank you again.