Post Expirator Shortcode

Below, you’ll find a detailed guide on how to add the Post Expirator Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Post Expirator Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Post Expirator Plugin and the shortcodes it provides:

Plugin Icon
PublishPress Future: Automatically Unpublish WordPress Posts

"PublishPress Future is a WordPress plugin designed to automatically unpublish posts after a set time. Ideal for time-sensitive content, it ensures your site stays up-to-date. Plugin slug: post-expirator."

★★★★✩ (147) Active Installs: 100000+ Tested with: 6.3.2 PHP Version: 7.2.5
Included Shortcodes:
  • [postexpirator]

Post Expirator [postexpirator] Shortcode

The Post Expirator shortcode allows you to set an expiration date for WordPress posts. The shortcode retrieves the expiration status and date of a post. If no expiration is set, it returns false. It also allows customization of date and time formats, and the type of expiration display (‘full’, ‘date’, or ‘time’). This functionality is useful for time-sensitive content, enabling automatic removal or archiving.

Shortcode: [postexpirator]

Parameters

Here is a list of all possible postexpirator shortcode parameters and attributes:

  • dateformat – Specifies the format of the date to be displayed.
  • timeformat – Specifies the format of the time to be displayed.
  • type – Determines if both date and time, only date, or only time should be displayed.
  • tz – Indicates the timezone for the expiration date and time.

Examples and Usage

Basic example – Displays the expiration date and time of a post using the default date and time format.

[postexpirator]

Advanced examples

Displays the expiration date of a post using a custom date format.

[postexpirator dateformat="F j, Y" type="date"]

Displays the expiration time of a post using a custom time format.

[postexpirator timeformat="g:i a" type="time"]

Displays the expiration date and time of a post using a custom date and time format.

[postexpirator dateformat="F j, Y" timeformat="g:i a"]

Displays the expiration date and time of a post using a custom format.

[postexpirator format="F j, Y @ g:i a"]

PHP Function Code

In case you have difficulties debugging what causing issues with [postexpirator] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode('postexpirator', 'postexpirator_shortcode');

Shortcode PHP function:

function postexpirator_shortcode($attrs)
{
    global $post;

    $container = Container::getInstance();
    $factory = $container->get(ServicesAbstract::EXPIRABLE_POST_MODEL_FACTORY);
    $postModel = $factory($post->ID);

    $enabled = $postModel->isExpirationEnabled();
    $expirationDateTs = $postModel->getExpirationDateAsUnixTime();
    if (! $enabled || empty($expirationDateTs)) {
        return false;
    }

    $attrs = shortcode_atts(
        array(
            'dateformat' => get_option('expirationdateDefaultDateFormat', POSTEXPIRATOR_DATEFORMAT),
            'timeformat' => get_option('expirationdateDefaultTimeFormat', POSTEXPIRATOR_TIMEFORMAT),
            'type' => 'full',
            'tz' => date('T'),
        ),
        $attrs
    );

    if (! isset($attrs['dateformat']) || empty($attrs['dateformat'])) {
        global $expirationdateDefaultDateFormat;
        $attrs['dateformat'] = $expirationdateDefaultDateFormat;
    }

    if (! isset($attrs['timeformat']) || empty($attrs['timeformat'])) {
        global $expirationdateDefaultTimeFormat;
        $attrs['timeformat'] = $expirationdateDefaultTimeFormat;
    }

    if (! isset($attrs['type']) || empty($attrs['type'])) {
        $attrs['type'] = 'full';
    }

    if (! isset($attrs['format']) || empty($attrs['format'])) {
        $attrs['format'] = $attrs['dateformat'] . ' ' . $attrs['timeformat'];
    }

    if ($attrs['type'] === 'full') {
        $attrs['format'] = $attrs['dateformat'] . ' ' . $attrs['timeformat'];
    } elseif ($attrs['type'] === 'date') {
        $attrs['format'] = $attrs['dateformat'];
    } elseif ($attrs['type'] === 'time') {
        $attrs['format'] = $attrs['timeformat'];
    }

    return PostExpirator_Util::get_wp_date($attrs['format'], $expirationDateTs);
}

Code file location:

post-expirator/post-expirator/legacy/functions.php

Conclusion

Now that you’ve learned how to embed the Post Expirator Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *