This quick tutorial will allow you to make contact page in Anchor 0.9.x
. Hope this help. By the way I'm currently in Anchor 1.9.0
Step 1:
Open /admin/pages
and create new page:
- Page title → Contact Me
- Page content → My contact page.
- Show in menu, Name → Up to you.
- Page slug → contact
- Status → Published
- Parent → None
Step 2:
Create new PHP file page-contact.php
and upload it to your theme folder /themes/your-theme-name
:
<?php theme_include('header'); ?>
<section class="content" id="content">
<!-- Your page title -->
<h2><?php echo page_title(); ?></h2>
<!-- Your page content -->
<div><?php echo page_content(); ?></div>
<!-- Notification message -->
<div><?php echo Notify::read(); ?></div>
<!-- The contact form -->
<form method="post" action="<?php echo current_url(); ?>">
<p><input name="contact-subject" type="text" placeholder="Hi!" value="<?php echo Input::previous('contact-subject'); ?>"> <label>Subject</label></p>
<p><input name="contact-name" type="text" placeholder="My name is…" value="<?php echo Input::previous('contact-name'); ?>"> <label>Your Name</label></p>
<p><input name="contact-email" type="email" placeholder="email@domain.com" value="<?php echo Input::previous('contact-email'); ?>"> <label>Your Email</label></p>
<p><textarea name="contact-message" style="width:100%;height:150px;" placeholder="Say something!"><?php echo Input::previous('contact-message'); ?></textarea></p>
<p><button type="submit">Send Message!</button></p>
</form>
</section>
<?php theme_include('footer'); ?>
Step 3:
Now open anchor/routes/site.php
then add the following code to the last line:
/**
* Contact page
*/
Route::post('contact', function() {
$input = Input::get(array('contact-subject', 'contact-name', 'contact-email', 'contact-message'));
// Validator check...
$validator = new Validator($input);
$validator->check('contact-subject')
->is_max(1, "Subject is required!");
$validator->check('contact-name')
->is_max(2, "Name is required!");
$validator->check('contact-email')
->is_email("Email is required!");
$validator->check('contact-message')
->is_max(5, "Message is empty or too short!");
if($errors = $validator->errors()) {
Input::flash();
Notify::error($errors);
return Response::redirect('contact#error');
}
$me = "your-email-address@domain.com"; // Your email address
$subject = $input['contact-subject'];
$message = $input['contact-message'];
$header = "From: " . $input['contact-email'] . " \r\n";
$header .= "Reply-To: " . $input['contact-email'] . " \r\n";
$header .= "Return-Path: " . $input['contact-email'] . "\r\n";
$header .= "X-Mailer: PHP \r\n";
if(mail($me, $subject, $message, $header)) {
Notify::success("Email sent!");
return Response::redirect('contact#sent');
} else {
Notify::error("Failed to send email!");
return Response::redirect('contact#failed');
}
});
Replace your-email-address@domain.com
with your email. The redirect URL and notify message can be changed up to you. For example, you want to redirect the user to the Thank You page if the email has been sent:
if(mail($me, $subject, $message, $header)) {
return Response::redirect('some-path/thank-you');
}
Click Save Changes. Done. Now open http://your_blog_domain.com/contact
. The contact page should be there!
PS:
I need help about making simple captcha or math question or picture question or security token or whatever to mimimize the spam email for this Anchor version. Anyone can give me a little guide?