Skip Navigation

[Gelöst] Change a user’s role with Cred Commerce on payment and post expiration

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

How to change a user's role when payment is made through Toolset Forms Commerce and then change it back again when the related post expires.

The API hook cred_commerce_after_order_completed can be used to trigger code which changes the user role using the standard set_role method.

Reverting the role when the related post expires is slightly trickier, as there is no Toolset API hook for when a post expires.

In this case, when a post expires it is changed to draft status.

You can use the standard WP hooks that are triggered when changing a custom post status to draft (e.g. the draft_project hook for a project post type) to check when a post of the relevant type is changed to draft status, and then check whether the post expiration time has passed (meaning the change to draft was likely triggered by the expiration) using the value stored in a hidden custom field '_cred_post_expiration_time' that can be retrieved with get_post_meta.

Relevant Documentation:

This support ticket is created vor 3 Jahre, 5 Monate. 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
- 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 4 Antworten, has 2 Stimmen.

Last updated by daveG-7 vor 3 Jahre, 5 Monate.

Assigned support staff: Nigel.


Hi. We have a CRED post form that accepts payment, giving people membership privileges that then expire after a year. Would it be possible for the system to change their user role when payment is accepted, then again when the post expires?



Languages: Englisch (English ) Spanisch (Español )

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

Hi Dave

You should be able to use the hooks provided by the Forms API to change the user role when the order completes.

Specifically, there is the cred_commerce_after_order_completed hook that you could use to trigger code that uses the set_role method of user objects:

It's less obvious what to do about changing the role back, because there is no hook available when the post expires. (It seems to me there should be, so I just created a request for one.)

You'll need to use built-in WordPress hooks that would be triggered by the action that occurs when the post is expired. So I would need to know what your form settings are for what happens when a post is expired, e.g. status is changed to...?


Thanks, that's a great start. As for expiration, the post status is changed to draft. However, there are other instances when a post is changed to draft where we probably wouldn't want the user role changed, so if it were possible to make it only happen to posts that were changed to draft by expiration, that would be ideal. However, as you say, there's no hooks for that, so if it's only possible when posts are changed to draft, that still might be better than it not changing it at all.



Languages: Englisch (English ) Spanisch (Español )

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

There are several WP hooks that could be used here (see, and I would be inclined to go with the {status}_{post_type} hook, which is triggered whenever the status of a post of the specified type changes to the specified status.

Not sure what your post type is, but say it were 'project', then the draft_project hook would be fired whenever a project post were changed to draft.

Now, this would catch all such post transitions, not just those where the post was expired.

But it should be simple enough to test the post expiration date against the current date-time. If the post expiration date has passed already, then we can assume the post was expired.

The post expiration date is available as a timestamp from the postmeta field with a key of '_cred_post_expiration_time'.


I think that points me in the right direction. If I encounter other issues further down the road, I'll start a new thread. Thanks.