Announcing a new addon/extension for USP Pro: Auto-Expire Posts! This addon enables users to submit posts that expire at a specific date and/or time. Very useful for things like contest sites, special offers, and social media promotional adventures.

How to use

Easy. Just add the shortcode [usp_post_expire] to your USP Form. It automatically will display a date-time field for the user to select a date. So there is no configuration required, but the shortcode provides plenty of attributes so you can customize things like the type of field, label text, and more. Here is a handy reference guide:

Shortcode: Date/Time Expiration Input

Returns markup for date/time field, hidden field, and some jQuery

Syntax: [usp_post_expire]

Attributes:
	type     = (optional)  type of input field (date, time, datetime-local) [default: date]
	label    = (optional)  text for associated input label                  [default: Date]
	required = (optional)  whether the input should be required             [default: true]
	fieldset = (optional)  enable fieldset: true, false, or custom class    [default: true]
	class    = (optional)  comma-separated list of classes                  [default: none]
	custom   = (optional)  any attributes or custom code                    [default: none]
	max      = (optional)  maximum number of allowed characters             [default: unlimited (999999)]
	error    = (optional)  custom name (displayed if there is an error)     [default: value of label attribute]

Example: [usp_post_expire type="datetime-local" label="Post Expiration Date" required="true" fieldset="true" max="999" error="Post Expiration Date"]

Here is a basic example of how it works:

  1. Add the shortcode to your form
  2. User selects a date and submits the post
  3. The post will expire on the specified date

So if the submitted post is “published”, it will be expired to “draft” status at the specified date. Otherwise, if the post is not published, then basically nothing will happen (other than the usp-custom-expires custom field will change to usp-custom-expired, which may be useful for certain implementations).

Get Auto-Expire Posts

Display the expiration date

After setting up auto-expiration, you may want to display the expiration date on the front-end posts. Here is a simple shortcode that can do it:

[usp_post_expire_date]

Add that shortcode to any submitted post or page to display the expiration date. If needed, it’s possible to customize the default date format. Just add the format attribute, like so:

[usp_post_expire_date format="Y-m-d"]

That will display the date like “2024-11-04”. You can include special characters, but they must be escaped with two percentage signs, like %%. For example, this shortcode:

[usp_post_expire_date format="l %%t%%h%%e jS"]

..will output something like “Wednesday the 5th”. Learn more about date expressions, how to format dates, and more at PHP.net.

Advanced: Custom Fields

By default, the shortcode generates one of three types of fields: date, time, or datetime-local. These are the standard HTML5 date-related input fields, and they cover a lot of use cases. But there may be situations where you need more control over the input field markup. Or maybe you don’t want to give the users a choice, and would rather just specify a static expiration date.

Expiration date with custom field

If you would rather use your own markup for the date/time input field, follow these steps.

Step 1: Add Custom Field

First, define your custom field and add it to the form. Here is an example that you can use as a starting point:

field#input|name#expires|type#datetime-local|label#Custom Date/Time Input|placeholder#Custom Date/Time Input

Make sure that the field attribute is input, and the name attribute is expires. Everything else may be customized as desired. Here is a shortcode reference for custom fields, providing all available attributes, etc.

Step 2: Add hidden field

Once you have defined your custom field and added it to the form, the next step is to add the required hidden field:

<input type="hidden" class="usp-post-expires" name="usp-post-expires" value="">

You can add this anywhere in the form content. No modifications are required.

Step 3: Add JavaScript

Lastly, add the required JavaScript to the form content, anywhere after the hidden field from step 2.

<script>
jQuery(document).ready(function($) {
	$('.usp-form').on('submit', function(e) {
		var usp_input = '#usp-custom-expires';
		if ($(usp_input).val().trim()) {
			var usp_expires = $(usp_input).val().replace('T', ' ');
			$('.usp-post-expires').val(usp_expires);
		}
	});
	$('.usp-error').each(function(index) {
		var text = $(this).text(); 
		$(this).text(text.replace('expires', 'Post Expiration'));
	});
});
</script>

Once you have completed all three steps, remember to save your changes. Then visit the form on the frontend and try submitting a few test posts to see how it works. Note that this tutorial provides the basic markup/JavaScript required; feel free to modify further if desired.

Specify a fixed/static expiration date

To set a fixed or static expiration date that will apply to all posts submitted via your USP Form, add the following code to the form content (on the “Edit USP Form” screen):

<input type="hidden" name="usp-post-expires" value="2017-11-15 07:04">

And then change the 2017-11-15 07:04 to the date/time at which you would like the submitted posts to expire. Note that the date must be written in Unix/POSIX format (which is a 24-hour format).

Advanced: Customizing

Auto-Expire Posts provides the following filter hooks for customizing plugin functionality:

usp_auto_expire_posts_status
usp_auto_expire_posts_time
usp_auto_expire_posts_time_now
Example 1

To customize the current date/time, add the following code snippet to your theme’s (or child theme’s) functions.php file, or add via simple custom plugin:

function usp_auto_expire_posts_time_now($time_now) {
	
	return $time_now; // modify as needed
	
}
add_filter('usp_auto_expire_posts_time_now', 'usp_auto_expire_posts_time_now');

This technique uses the usp_auto_expire_posts_time_now filter hook to pass the desired date/time to the expiration function. This enables the plugin to be more precise when determining whether or not a post has reached the specified expiration date. So you can compensate for any time-zone differences, etc.

Example 2

To customize the post status for expired posts, add the following code snippet to your theme’s (or child theme’s) functions.php file, or add via simple custom plugin:

function usp_auto_expire_posts_status($status) {
	
	return $status; // modify as needed
	
}
add_filter('usp_auto_expire_posts_status', 'usp_auto_expire_posts_status');

This technique uses the usp_auto_expire_posts_status filter hook to assign the desired post status to expired posts. So instead of using the default post status of “Draft”, you can set expiring posts to “Pending” or “Trash” or whatever status makes sense.

Get this extension

Get Auto-Expire Posts

Check out more extensions »