Skip Navigation

[Resolved] Feature Request: Extend Custom Actions When Post Expires

This support ticket is created 6 years, 1 month 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)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by julieP 6 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1127304

I'd like to be able to more than just update a single custom field with the cred_post_expiration_custom_actions hook.

I'd like to be able to:-

1. update one (or more) custom field values
2. change the User's role
3. change the expiry date (extend it)

I'm personally not too bothered about being able to manipulate the post status but I can see why someone else would.

If these actions could be set up via the post expiration hook, it would give us many more options and flexibility in the design and functionality of our sites (and without the need for manual intervention).

#1127345

Hi Julie,

Thank you for contacting us and I'll be happy to assist.

The way "cred_post_expiration_custom_actions" hook works, it is executed at the time when the CRED form is submitted and a post is created.

Only the custom field values passed through "$custom_actions" array will be updated through the CRON, at the time of post-expiry.
( ref: https://toolset.com/documentation/user-guides/automatic-post-expiration/#what-happens-when-posts-expire )

You can update one or more custom field values through it, e.g:


add_filter('cred_post_expiration_custom_actions', 'my_custom_actions', 10, 3);

function my_custom_actions($custom_actions, $post_id, $form_data) {
	
	$custom_actions[] = array( 'meta_key' => 'custom_field_key_1', 'meta_value' =>'custom_field_value_1' );
	$custom_actions[] = array( 'meta_key' => 'custom_field_key_2', 'meta_value' =>'custom_field_value_2' );
	$custom_actions[] = array( 'meta_key' => 'custom_field_key_3', 'meta_value' =>'custom_field_value_3' );

	return $custom_actions;
}

To change the user role at the time of post expiry, you can make use of "Post Status Transitions" hooks.

1. You'll first register a new post status, e.g. "Expired" ( https://codex.wordpress.org/Function_Reference/register_post_status ), and set it for "After expiration change the status of the post to" option in the CRED form.
( ref: https://toolset.com/documentation/user-guides/automatic-post-expiration/#enabling-automatic-expiration-of-posts )

2. After that, you can create a custom function that is hooked to execute when post's status is changed from non-expired to expired and in that, you can change the user's role.

To change/extend the post expiry date programmatically, complex custom code involving WordPress' "Transients API" ( https://codex.wordpress.org/Transients_API ) will be needed. I'm afraid, custom programming such as this is beyond the scope of support that we provide, but you can consult one of our certified consultants for this:
hidden link

I hope these points will help and please let me know if there's anything else I can help you with.

regards,
Waqar

#1127366

Hi Waqar
Thank you for this.

I'm happy to look into the proposed method for updating the User role (appreciate it falls outside toolset's scope for support).

My understanding is that the custom actions will run BEFORE the expiration actions set by the form which means that if I include the meta key _cred_post_expiration_time in the hook this will actually be overwritten to 0 by the form itself. Have I misunderstood this?

#1127383

Hi Julie,

Thanks for writing back.

Your understanding is correct and if "_cred_post_expiration_time" key value is passed on through "$custom_actions", it will be overwritten to "0".

It can be updated through a different function, for example from a one that is hooked to post's status change event ( Post Status Transitions ). But it is important to note that only updating that custom field value won't be enough and you'll still need to set a related transient to make the WordPress CRON keep track of when the next expiry check needs to be performed.
( you can check the code in the file: wp-content/plugins/cred-frontend-editor/library/toolset/cred/embedded/classes/CredPostExpiration.php to see how the plugin manages them ).

To make your custom developed solution future safe, my recommendation would be to use own custom field values (e.g. "is_extended", "future_post_expiration_time" etc) and custom transients to keep track of post's status extensions, instead of trying to hook on to the CRED plugin's own (built-in) custom fields and transients.

If I can be of any further assistance, please let me know.

regards,
Waqar

#1127995

Thank you for explaining.