What's the Current Year IV

Forwarding users to a year-dependent page automatically

In the previous article, we looked at how to control the content on a page based on the year. In this one we'll see a way to forward users with links based on the year. In this one we'll see how to automatically forward users to a year-based page.

MODX logo

The Problem

There might be situations where you want to automatically forward users to a year-based page without making them click on a link, for example an automatic forward to the current year's events when they log in.

The Solution

As before, you'll want to have the alias of each resource contain the year like this: events2017, events2017, events2017.

You can create a landing page and use its ID in the &redirectTo property of the Login snippet.

Put this tag (and nothing else) on the landing page:


Now create a snippet called RedirectToCurrent with this code:

/* RedirectToCurrent snippet */
$year = date("Y");
$alias = 'events' . $year;
$resource = $modx->getObject('modResource', array('alias' => $yearToShow));

if ($resource) {
    $docId = $resource->get('id');
} else {
        '[RedirectToCurrent snippet] resource with alias ' .
        $alias . ' not found: ');

$url = $modx->makeUrl($docId, "", "", "full");

The user will always be redirected to the current year page.

Coming Up

In the next article, we'll look at an odd example of resources that save fine with no errors, but don't show up in the Resource tree.

For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.