Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Php and WordPress.

We know there are hundreds of plugins to let you host a contact form in WordPress, but I am going to show you how to create your own one.

We are about to create a custom contact page that will work under WP 3.0, and more precisely with Twenty Ten theme. Obviously, with some minor changes you will be able to do the same in your theme.

The script

Let’s start: in your theme folder, create a new file called contact.php (or whatever other name with php extension) and write this code:

Please enter your name

"; } if(!filter_var(trim($_POST[your_email]),FILTER_VALIDATE_EMAIL)){ $error .= "

Please enter a valid email address

"; } if(!trim($_POST[your_message])){ $error .= "

Please enter a message

"; } if(!$error){ $email = mail(get_option("admin_email"),trim($_POST[your_name])." sent you a message from ".get_option("blogname"),stripslashes(trim($_POST[your_message])),"From: ".trim($_POST[your_name])." <".trim($_POST[your_email]).">\r\nReply-To:".trim($_POST[your_email])); } } ?>
>

Message succesfully sent. I'll reply as soon as I can

Your messange hasn't been sent

Now let’s see what we’ve done:

Lines 2-4: This is the way we assign a template name to the page. In this case, the name is Contact me

We’ll see later what lines 5-19 do, and let’s jump to…

Lines 21-27: this is the same content you can find in page.php file into Twenty Ten theme folder, and if you’re using a different theme it should be the same content you can find in your theme page.php file until the beginning of the content of the page (typically the_content()).

In my case, I just replaced


	

with

because I know a contact page can’t be the front page.

Lines 28-33: Here come to play two variables, called $error and $email, you’ll see later how to set their values, meanwhile you have to know $error is an empty string or a string with an error (something like “You email address is not valid”) and $email is true if the email has been successfully sent.

The code proceeds this way:

* If the email has been sent, show a “Thank you message” and nothing else

* If the email has not been sent and there is an error, show the error and the form

* If the email has not been sent and there isn’t an error, show the page content (something like “Hello!! This is my fancy contact page!! Drop me a mail!!) and the form

Lines 34-53: The form itself, the easiest possible, it’s up to you to add some more fields, if needed, and some styling.

Lines 55-61: The end of the page, like in the normal Twenty Ten page.

Now it’s time to see the php that handles emails and errors

Line 5: If “Send email” button (line 50) has been pressed…

Line 6: set $error variable to empty string

Lines 7-9: creating an error message if the user did not enter his name

Lines 10-12: same as before, looking for a valid email address

Lines 13-15: same as before, looking for a message

Lines 16-18: if I did not find any error, I send the email with a basic formatting and sanitizing.

The setup

Once you created this file, create a new page this way:

Look how I assign the page template according to the file recently created.

And this is what you’ll get:

Your own custom contact page. It’s just a basic one, but if I receive good feedback, I can improve it with some jQuery magic…

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.