Publish page in future (Datepicker)

Silverstripe Version: 4.9.0

Question: Hello everyone, does anyone have an idea a simple way how to publish page in the future?

i think i should make endpoint and set the cron to call this endpoint and save the page to Live_ but there is no any easier way to get this done? maybe any function to checking if the page is published ?

im thinking about function to check if page was published but it can be only done when i entering this page and it didint work for example if i have listing for this pages.

im checking in the internet but every question is very old and can be hard to implement this with 4.9.0 version.

if someone have any good idea please help me :slight_smile:

If you don’t want to complicate with tasks / cronjob, you could add some field which will define, when page can be accessed for example “DatePublishedFrom”, and then compare with current date.

I would do something like this:

in Page.php

use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Forms\DatetimeField;

private static $db = [
    'DatePublishedFrom' => DBDatetime::class,
];

public function getCMSFields()
{

    $fields->addFieldsToTab('Root.Main', [
            DatetimeField::create('DatePublishedFrom', 'Publish from'),
        ]
    );

    // Check if publish date is greater than or equal current.
    public function allowVisitingPublishedSite()
    {
        if ($this->DatePublishedFrom) {
            $now = date('Y-m-d H:i:s');
            if ($now >= $this->DatePublishedFrom) {
                return true;
            }
        }
        return false;
    }

}

in PageController

use SilverStripe\Security\Permission;

protected function init()
{
    parent::init();
    if (!Permission::check('ADMIN') && !$this->allowVisitingPublishedSite()) {
        $this->redirectBack();
    }
}

and for your listing page you can use filter

$yourList->filter([
     'DatePublishedFrom:GreaterThanOrEqual' => $now,
]);

not sure if that’s what you’re asking, I hope it helps:)

1 Like

The SilverStripe BlogPost page type already has this functionality. I suggest using this as a guide on how to best achieve what you are after.