Coding a button linking to an external site that changes monthly

There are probably several ways to solve this. Here is a general outline of what popped into my mind first.

  • Create a custom plugin to house your custom feature code and to make it independent of the used theme
  • Register a custom admin menu page to serve as a settings page
  • Register a custom setting, displayed on the above page, for storing the current registration link and to allow manual override/fixing
  • Schedule a cron action to update the above setting automatically – this could be a monthly or even a daily check
  • Create a helper function to return the registration url from the setting for reuseability
  • Create a shortcode or a dynamic block for rendering the sign up button, which uses the above helper function for getting the url

If you want to make the redirect look more WordPress-y, then you could use wp_redirect(), which is a wrapper for header() with status code and X-Redirect-By added.

Personally I wouldn’t write a code which computes the correct url again and again on every page load as that is a waste of resources in my opinion. Especially as the url is the same for a month at a time.

But if you want to determine the url every time, then I’d would use an array to serve as a map and pull the correct url out of it with an array key. Something along these lines.

$current_month = (int) date('n');
$monthly_urls = array(
    1 => 'januaryurl',
    2 => 'februaryurl',
    // ...
);

$this_months_url = $monthly_urls[$current_month];

error code: 523