Jump to content

PHP Code to Display One of Two WordPress Pages Based on Today's Date


Recommended Posts

I am a PHP newbie, and although I have done some programming (not a lot) in the distant past (Pascal, Fortran, Assembly, dabbled in C slightly,) please forgive me if this is a classic "newbie's" dumb question.

 

I just implemented a WordPress-based online registration form for an upcoming event. Registration will be closing on a specific date, and I am trying to create a PHP routine with the following logic:

  • If registration is open, the Registration Form should be displayed;
  • If registration is closed, a Registration is Closed page/message should be displayed.

I have the date comparison code written and it works like a charm provided all I do is echo "hard coded" text to the screen.

 

Both the Registration Form and the Registration is Closed pages are WordPress pages within my site and can be accessesd via Permalinks/URLs.

 

Although I could hardcode the content of each of these two pages into my PHP routine, I would prefer to point to the two pages to facilitate future content updates/changes/additions/etc.

 

Additionally, I would like to implement this routine as a ShortCode which would be placed on its own WordPress page. The page containing the shortcode would be accessed via a Menu Tab on my page - when the tab is clicked (i.e., someone wants to register,) the PHP routine would ascertain the status of Registration (i.e., open or closed,) and then display the appropriate page.

 

Here is my code as it stands now (with text hardcoded within the echo construct rather than my pages' urls...):

 

<?php
/* This compares today's date with the date registration closes.
If registration is open, the registration page will be displayed.
If registration is closed, the registration is closed page is displayed.
*/

$reg_close_date = "2013-04-08";
$todays_date = date("Y-m-d");

$today = strtotime($todays_date);
$registration_closed_date = strtotime($reg_close_date);

if ($registration_closed_date >= $today){
 echo 'display registration form';
 }
else {
 echo 'display registration is closed form';
 }
?>

 

As I mentioned earlier, I am a PHP newbie, and I GREATLT appreciate any help anyone can offer... For all I know, what I would like to do is not possible, although I suspect my use of echo constructs may be the incorrect choice for what I want to achieve.

 

Thanks in advance!

Nancy

Edited by nancygraz

I just reread my original post and realized that I neglected to make clear what my question/difficulty is... DOH!

 

[FWIW, I am using a WP plugin (i.e., Shortcode Exec PHP,) to define my shortcode - this plugin is perfect for my particular needs at this point. I am sure down the road I will graduate to doing this the proper way as described in mweldan's response. :-) ]

 

Anyway, here is my question for everyone:

 

How should I code the two urls for the pages I want displayed?

 

So far I have tried using two echo constructs similar to the following (sample) to display the desired webpages:

 

 echo '<a href="http://url_to_wp_page.com"></a>';

 

This did not work - nothing was displayed. Just got a pretty white page...

 

But when I echo text alone, everything works perfectly, and the text string is displayed as expected.

 

I am obviously either using the wrong method to "display" the pages, or I have not used the proper syntax for the echo construct...

 

Anyone have any ideas?

 

Thanks again!

Nancy

in wordpress, you have to create a page from admin panel to get a page with its own url:

 

eg: http://host.tld/pagename

 

i would assume you know this.

 

to answer your question,

 

you can put page file in your theme directory and use include() or anything related .

 

i would suggest to redirect to its own page, but then you will have to create page for each content,

 

once page has been created, usually it will be displayed in frontend menu, you will have to do something to hide it.

I really appreciate your trying to help - I guess I am having trouble describing/conveying what it is I am asking. :-(

 

I already have my two target pages created, hidden, and I know what their specific urls are.

 

What I cannot figure out is the specific command to use to display the desired page.

 

I have tried using an echo construct formatted as shown above in my previous post. Although I provided a "dummy" url in my sample code above, I used my actual page urls when testing my code. When I format my target urls within the echo construct as shown in my sample code, the expected page is not displayed - all I get is a blank page. I suspect I am either not formatting the urls properly for use in the echo construct, or I should be using another command to display the pages.

 

Hopefully third time is the charm and I have explained myself better this time... :-D

 

Thanks again!

Nancy

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.