0

I've seen some similar questions, but they are not totally the same. If I missed in my search, please forgive me. However this what I have so far:

I've created a form that has an input field for the access code and a submit button, as follows:

<form action="#" method="POST" name="access">
    <p style="text-align: center;">
        <input id="access_code" type="Text" name="access_code" value="" />
        <input type="Submit" name="access1" value="Register" />
    </p>
</form>

Then on the backend, I started with something like this:

if(isset($_POST['Register'])){
    if(empty($_POST['access_code'])){
        echo "Access Code Must Be Entered";
    }
}

$DisplayForm = True;
if(isset($_POST['Register'])){
    $DisplayForm = False;
    echo $_POST['access_code'];
}

if ($DisplayForm){
    ?>
    <form action="#" method="POST" name="access">
        <input type="Text" name="access_code" value=" " />
        <input type="Submit" name="access1" value="Register" />
    </form>
    <?php
}

echo "<a href="http://www.mysite.com/<?php echo $_POST['access_code'];?>"<img src="http://www.mysite.com/images/logo.jpg"></a>"

I realize my final echo syntax is broken, but not quite sure how to write out.

All I want to do is have this form redirect a user to a custom page that's based on the access code they have been given and call it with a short code. For example, if the access code is "help", they are sent to a page of http://www.mysite.com/help, or if the password is "contact", they are then sent to a page of http://www.mysite.com/contact.

I initially wanted to base this on the password protection feature in WordPress, where if the page password was also "help" it would just redirect to that URL, but I may be over complicating things.

So the new code should look like this:

if(isset($_POST['Register'])){
    if(empty($_POST['access_code'])){
        echo "Access Code Must Be Entered";
    }
}

$DisplayForm = True;
if(isset($_POST['Register'])){
    $DisplayForm = False;
    echo $_POST['access_code'];
}

if ($DisplayForm){
    ?>
    <form action="/" method="$_GET" name="access">
        <input type="Text" name="name" value="" />
        <input type="Submit" name="access1" value="Register" />
    </form>
    <?Php
}

wp_redirect(get_bloginfo('url').'/'.$_POST['access_code']);

I think I understand it all except the execution of the before get_header() part. How would I do that?

  • 1
    This sounds like a great idea for a plugin, and is going to take quite a bit of work if you are new to plugin development. I would suggest reading about how to register shortcode. The rest of the work is going to be cleansing the $_POST variable to make sure its valid, and then using wp_redirect() to go to the permalink of the page in question. Sounds like a fun project, honestly! – GhostToast Jan 8 '14 at 15:37
  • 1
    This question is far too broad for the SE Q & A format. Please show what you have done to research a possible implementation, what code you have attempted, and what issues you have observed with your attempted code. – Chip Bennett Jan 8 '14 at 15:52
  • I added additional code for what I started with for it on the backend. It would come up easy. I just don't know how to tie it all together and fix my code that actually prints the address. I wanted to make it a plugin, if someone wants to help, I'm patient. According to my internet search there were quite a few people besides me looking for it. I couldn't find it, so I said let me try building it. This would be my first one. – Shanne69 Jan 8 '14 at 16:35
  • I did research how to make into a shortcode by adding it to the functions.php once I have the code. I just need gather the code. – Shanne69 Jan 8 '14 at 19:18
0

If your code is truly going to be the slug ("secret-page" = example.com/secret-page), you could do something like this...

wp_redirect(get_bloginfo('url').'/'.$_POST['access_code']);

But realize that headers cannot have already been sent, so this needs to happen before get_header(). Some more testing would be needed, but that should get you down the right path in principle.

You could also have the form's action be '/', method as $_GET and make the name of the input be "name" (<input name="name" ...>).

So if you visit

example.com/?name=my-secret-page

you will end up at

example.com/my-secret-page

as that is default behavior when using "pretty" permalinks

  • I Got the script to Run with a short code under using the plugin Shortcode Exec PHP! Even though the script is running perfectly, I still get the Warning: Cannot modify header information - headers already sent by (output started at /mysite.com/html/wp-content/themes/theme/header.php:4) in /mysite.com/html/wp-includes/pluggable.php on line 896 Any ideas on how to fix this one? – Shanne69 Jan 9 '14 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.