Có phải bạn muốn thiết kế website tiết kiệm chi phí và thời gian mà còn chuẩn seo ?

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

Thiết kế website bằng wordpress

Với các gói 500k, 750k, 999k là bạn co ngay 1 website đẹp, chuẩn seo

10/30/13

Hiển thị danh sách "post_type" trong wordpress



Hàm lấy danh sách post type theo tên post_type
<?php
        $args = array(
                                'numberposts' => 0,
                                'post_type'   => 'country-origin'
                            );
         $result = get_posts($args);                           
?>

Hàm lặp và hiển thị danh sách
<?php
     foreach($result as $row){
 ?>
<option value="<?=$row->ID?>"><?=$row->post_title?></option>
<?}?>


Ghi chú:
- $row->ID: Trả về ID
- $row->post_title: Trả về tên của post type



Wordpress lấy danh mục - get categories

Description

Returns an array of category objects matching the query parameters.
Arguments are pretty much the same as wp_list_categories and can be passed as either array or in query syntax.

Usage

 <?php $categories get_categories$args ); ?> 

Default Usage

<?php 

$args = array(
	'type'                     => 'post',
	'child_of'                 => 0,
	'parent'                   => '',
	'orderby'                  => 'name',
	'order'                    => 'ASC',
	'hide_empty'               => 1,
	'hierarchical'             => 1,
	'exclude'                  => '',
	'include'                  => '',
	'number'                   => '',
	'taxonomy'                 => 'category',
	'pad_counts'               => false 

); 

?>

Parameters

type 
(string) Type of category to retrieve
  • post - default
  • link
Note: type=link has been deprecated from WordPress 3.0 onwards. Use taxonomy=link_category instead.
child_of 
(integer) Display all categories that are descendants (i.e. children & grandchildren) of the category identified by its ID. There is no default for this parameter. If the parameter is used, the hide_empty parameter is set to false.
parent 
(integer) Display only categories that are direct descendants (i.e. children only) of the category identified by its ID. This does NOT work like the 'child_of' parameter. There is no default for this parameter. [In 2.8.4]
orderby 
(string) Sort categories alphabetically or by unique category ID. The default is sort by Category ID. Valid values:
  • id
  • name - default
  • slug
  • count
  • term_group
order 
(string) Sort order for categories (either ascending or descending). The default is ascending. Valid values:
  • asc - default
  • desc
hide_empty 
(boolean) Toggles the display of categories with no posts. The default is 1 for true or you can add '0' for false (show empty categories). Valid values:
  • 1 - default
  • 0
hierarchical 
(boolean) When true, the results will include sub-categories that are empty, as long as those sub-categories have sub-categories that are not empty. The default is true. Valid values:
  • 1 (true) - default
  • 0 (false)
exclude 
(string) Excludes one or more categories from the list generated by wp_list_categories. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example.
include 
(string) Only include certain categories in the list generated by wp_list_categories. This parameter takes a comma-separated list of categories by unique ID, in ascending order. See the example.
  • list - default.
  • none
number 
(string) The number of categories to return
taxonomy 
(string or array) Taxonomy to return. This parameter added at Version 3.0 Valid values:
  • category - default
  • taxonomy - or any registered taxonomy
pad_counts 
(boolean) Calculates link or post counts by including items from child categories. Valid values:
  • 1 (true)
  • 0 (false) - default

Return values

(array) 
Returns an array of category objects matching the query parameters.
The complete content of $category is:
$category->term_id
$category->name
$category->slug
$category->term_group
$category->term_taxonomy_id
$category->taxonomy
$category->description
$category->parent
$category->count
$category->cat_ID
$category->category_count
$category->category_description
$category->cat_name
$category->category_nicename
$category->category_parent

Examples

Dropdown Box as used in Parent category at post category page

This is the code used in the build in category page. Code from 3.0.1
wp_dropdown_categories(array('hide_empty' => 0, 'name' => 'category_parent', 'orderby' => 'name', 'selected' => $category->parent, 'hierarchical' => true, 'show_option_none' => __('None')));
This slightly altered code will grab all categories and display them with indent for a new level (child category). The select box will have a name= and id= called 'select_name'. This select will not display a default "none" as the original code was used to attach a category as a child to another category (or none).
wp_dropdown_categories(array('hide_empty' => 0, 'name' => 'select_name', 'hierarchical' => true));

Dropdown Box

Here's how to create a dropdown box of the subcategories of, say, a category that archives information on past events. This mirrors the example of the dropdown example of wp_get_archives which shows how to create a dropdown box for monthly archives.
Suppose the category whose subcategories you want to show is category 10, and that its category "nicename" is "archives".
<select name="event-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> 
 <option value=""><?php echo esc_attr(__('Select Event')); ?></option> 
 <?php 
  $categories = get_categories('child_of=10'); 
  foreach ($categories as $category) {
  	$option = '<option value="/category/archives/'.$category->category_nicename.'">';
	$option .= $category->cat_name;
	$option .= ' ('.$category->category_count.')';
	$option .= '</option>';
	echo $option;
  }
 ?>
</select>

List Categories and Descriptions

This example will list in alphabetic order, all categories presented as links to the corresponding category archive. Each category descripition is listed after the category link.
<?php
$args = array(
  'orderby' => 'name',
  'order' => 'ASC'
  );
$categories = get_categories($args);
  foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  } 
?>

Get only top level categories

To get the top level categories only, set parent value to zero. This example gets link and name of top level categories.
<?php
$args = array(
  'orderby' => 'name',
  'parent' => 0
  );
$categories = get_categories( $args );
foreach ( $categories as $category ) {
	echo '<a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a><br/>';
}
?>

Source File

get_categories() is located in wp-includes/category.php.

10/29/13

WordPress & jQuery Contact Form without a Plugin



There are lots of WordPress plugins for contact forms, but wouldn’t it be nice to have more control over the markup? In this tutorial, I am going to show how to use a custom page template to create a contact form in WordPress without a plugin.


Some people may want to skip the post and get to the demo and source files:


View Demo Download Source Files
So, why not use a plugin?


Well, I think that a contact form is so simple that a WordPress plugin that doesn’t provide exactly what I want is pretty lame. Plus, I don’t need all those fancy interfaces for building the form; I just need the code.
The Plan


Our plan is to create a custom WordPress Page Template, then create a page that uses that template. Finally, we will add in a little jQueryto improve the form. Let’s write a little pseudo code to help determine how our page template will be structured.
Pseudo Code
If form was submitted validate it
If the form was submitted successfully
Send email(s)
Else
Set variables to show errors

If the form was submitted successfully
Show thank you message
Else
Show form (with errors if there are any)

Creating the WordPress Page Template


Alright, so first, we are going to start with some basic stuff: define the template name, include the header/sidebar/footer, and setup the basic structure of our pseudo code.
<?php
/*
Template Name: Contact Form
*/
?>

<?php
//If the form is submitted
if(isset($_POST['submitted'])) {
//If there is no error, send the email
if(!isset($hasError)) {

}
} ?>

<?php get_header(); ?>

<?php
//If the email was sent, show a thank you message
//Otherwise show form
if(isset($emailSent) && $emailSent == true) {
?>

<?php } else { ?>

<?php } ?>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

The Form


Next, let’s code the actual form. I also want to provide the ability for the user to enter some text to go above the form, so we are going to use the regular loop:
<?php if(isset($emailSent) && $emailSent == true) { ?>

<div class="thanks">
<h1>Thanks, <?=$name;?></h1>
<p>Your email was successfully sent. I will be in touch soon.</p>
</div>

<?php } else { ?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>

<?php if(isset($hasError) || isset($captchaError)) { ?>
<p class="error">
There was an error submitting the form.
<p>
<?php } ?>

<form action="<?php the_permalink(); ?>" id="contactForm" method="post">

</form>

<?php endwhile; ?>
<?php endif; ?>
< } ?>



If the emailSent variable is set to true, we display the thank you message. Otherwise, we show the form. So we are outputting the title of the page, and any content that was entered. Then, we are checking to see if there was an error. Finally, we display the form:
<ol class="forms">
<li>
<label for="contactName">Name</label>
<input type="text" name="contactName" id="contactName" value="
<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>"
class="requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</li>

<li>
<label for="email">Email</label>
<input type="text" name="email" id="email" value="
<?php if(isset($_POST['email'])) echo $_POST['email'];?>"
class="requiredField email" />
<?php if($emailError != '') { ?>
<span class="error"><?=$emailError;?></span>
<?php } ?>
</li>

<li class="textarea">
<label for="commentsText">Comments</label>
<textarea name="comments" id="commentsText" rows="20" cols="30" class="requiredField">
<?php if(isset($_POST['comments'])) {
if(function_exists('stripslashes')) {
echo stripslashes($_POST['comments']);
} else {
echo $_POST['comments'];
}
} ?></textarea>
<?php if($commentError != '') { ?>
<span class="error"><?=$commentError;?></span>
<?php } ?>
</li>
<li class="inline">
<input type="checkbox" name="sendCopy" id="sendCopy" value="true"
<?php if(isset($_POST['sendCopy']) && $_POST['sendCopy'] == true)
echo ' checked="checked"'; ?>
/>
<label for="sendCopy">Send a copy of this email to yourself</label>
</li>
<li class="screenReader">
<label for="checking" class="screenReader">If you want to submit this form, do not enter anything in this field</label>
<input type="text" name="checking" id="checking" class="screenReader" value="
<?php if(isset($_POST['checking'])) echo $_POST['checking'];?>"
/>
</li>
<li class="buttons">
<input type="hidden" name="submitted" id="submitted" value="true" />
<button type="submit">Email me »</button>
</li>
</ol>



Note: Line wrapping added for readability.


Ok, wow, that may seem like a lot. So let’s break down the name field to see what this code is actually doing:
<li><label for="contactName">Name</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</li>



We are displaying each form field in a list item, displaying the label, input field, and then showing an error message if there is one. We are also displaying the value in the form field if it was already submitted.


So in essence, we are just doing that for each field of the form. You could easily go into the code to add more fields.


We are also using honeypot captcha to see if a bot was trying to submit the form:
<li class="screenReader">
<label for="checking" class="screenReader">If you want to submit this form, do not enter anything in this field</label>
<input type="text" name="checking" id="checking" class="screenReader" value="<?php if(isset($_POST['checking'])) echo $_POST['checking'];?>" />
</li>



That list item is pushed off the page with CSS, and if there is a value in that field, we can assume that it is not a human trying to submit the form:
.screenReader {
left: -9999px;
position: absolute;
top: -9999px;
}

The Validation


Next, we are going to have a conditional statement to determine if the form was submitted or not:
<?php
//If the form is submitted
if(isset($_POST['submitted'])) {

} ?>



Within that conditional, is where we are going to do all of our validation of the required fields. First, let’s check to see if our honeypot captcha form field is filled in. If it is, then we will display an error and not check anymore of the form:
//Check to see if the honeypot captcha field was filled in
if(trim($_POST['checking']) !== '') {
$captchaError = true;
} else {

}



So if the captcha field was left blank, we will continue to validate the required fields:
//Check to make sure that the name field is not empty
if(trim($_POST['contactName']) === '') {
$nameError = 'You forgot to enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) === '') {
$emailError = 'You forgot to enter your email address.';
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}quot;, trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}

//Check to make sure comments were entered
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter your comments.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}



Again, that may seem like a little much, so let’s just take a look at the validation on the name field:
if(trim($_POST['contactName']) === '') {
$nameError = 'You forgot to enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}



If the name field if empty, set a variable that will display the name error and set a flag saying that there was an error on the form. Otherwise, set a variable that will contain the name value from the form.
Sending the Email


Now, we want to send the email if there are no errors:
//If there is no error, send the email
if(!isset($hasError)) {

$emailTo = 'me@somedomain.com';
$subject = 'Contact Form Submission from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name nnEmail: $email nnComments: $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "rn" . 'Reply-To: ' . $email;

mail($emailTo, $subject, $body, $headers);

if($sendCopy == true) {
$subject = 'You emailed Your Name';
$headers = 'From: Your Name <noreply@somedomain.com>';
mail($email, $subject, $body, $headers);
}

$emailSent = true;

}



Note: Items bolded are values that you will want to change before implementing.


I personally like to have the contact form submission come from myself. That way, I just setup a filter in Gmail, and I can guarantee that it won’t get caught in my spam filter. The second email is only sent if the user checks the checkbox to send a copy to themselves.


View Entire Custom Page Template
Creating the Page in WordPress


First, you want to upload the contact-form.php page template to your themes folder. Next, you will create the page in WordPress and selectContact Form as the page template.





Then, just publish your page and you will have your contact form. Well, the form will be usable, but we are going to add in some CSS and jQuery to finish it off.
Styling the Form


I recently wrote an article about styling forms, so we are just going to stick with some basic styles:
.screenReader { left: -9999px; position: absolute; top: -9999px; }
.thanks { background: #F2F3F6; border: 1px solid #7E8AA2; padding: 10px; }

/*****Forms*****/
ol.forms { float: left; list-style: none; margin: 0; width: 100%; }
ol.forms li {
clear: both;
float: left;
margin-bottom: 18px;
position: relative;
width: 100%;
}
ol.forms label {
cursor: pointer;
display: block;
float: left;
font-weight: bold;
padding-right: 20px;
width: 100px;
}
ol.forms input, ol.forms textarea {
border: 1px solid #7E8AA2;
border-radius: 3px;
font: inherit;
-moz-border-radius: 3px;
padding: 2px;
-webkit-border-radius: 3px;
width: 214px;
}
ol.forms textarea { height: 300px; width: 334px; }
ol.forms input:focus, ol.forms textarea:focus { background-color: #f2f3f6; border-color: #ff9800; }
.error { color: #f00; }
ol.forms li .error { font-size: 12px; margin-left: 20px; }
ol.forms li.textarea .error {
display: block;
position: absolute;
right: 0;
top: 0;
width: 100px;
}
ol.forms li.screenReader { margin-bottom: 0; }
ol.forms li.buttons button {
background: #ff9800;
border: none;
color: #000;
cursor: pointer;
font: 16px/16px "Avenir LT Std", Helvetica, Arial, sans-serif;
overflow: hidden;
padding: 6px 3px 3px 3px;
text-transform: uppercase;
width: auto;
}
ol.forms li.buttons button:hover { color: #222; }
ol.forms li.buttons button:active { left: -1px; position: relative; top: -1px; }
ol.forms li.buttons, ol.forms li.inline { float: right; width: 460px; }
ol.forms li.inline input { width: auto; }
ol.forms li.inline label { display: inline; float: none; width: auto; }



So drop that CSS in your theme stylesheet and you will see the form start to look much better.
Enhancing the form with some jQuery


I have also already written about creating AJAX forms with jQuery, but I thought I would specifically talk about an AJAX script for a contact form.


First, we want to execute our jQuery when the document is ready and the form was submitted:
$(document).ready(function() {
$('form#contactForm').submit(function() {

return false;
});
});



Next, we want to hide any error messages if there are any and validate any required fields which are denoted with a class of requiredField:
$('form#contactForm .error').remove();
var hasError = false;
$('.requiredField').each(function() {

});



After that, we want to validate that the field is not empty and append an error message if it is:
if(jQuery.trim($(this).val()) == '') {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">You forgot to enter your '+labelText+'.</span>');
hasError = true;
}



If the field is not empty and also has a class of email, we want to validate that the email address is valid:
else if($(this).hasClass('email')) {
var emailReg = /^([w-.]+@([w-]+.)+[w-]{2,4})?$/;
if(!emailReg.test(jQuery.trim($(this).val()))) {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">You entered an invalid '+labelText+'.</span>');
hasError = true;
}
}



If there are no errors, then we want to replace the submit button with a loading image:
if(!hasError) {
$('form#contactForm li.buttons button').fadeOut('normal', function() {
$(this).parent().append('<img src="/wp-content/themes/td-v3/images/template/loading.gif" alt="Loading…" height="31" width="31" />');
});



Note: You will need to update the source of the image to wherever you upload it in your theme.


Finally, let’s submit the form via an AJAX request, slide the form up, and show a thank you message:
var formInput = $(this).serialize();
$.post($(this).attr('action'),formInput, function(data){
$('form#contactForm').slideUp("fast", function() {
$(this).before('<p class="thanks"><strong>Thanks!</strong> Your email was successfully sent. I check my email all the time, so I should be in touch soon.</p>');
});
});



Take a look at the entire script:
$(document).ready(function() {
$('form#contactForm').submit(function() {
$('form#contactForm .error').remove();
var hasError = false;
$('.requiredField').each(function() {
if(jQuery.trim($(this).val()) == '') {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">You forgot to enter your '+labelText+'.</span>');
hasError = true;
} else if($(this).hasClass('email')) {
var emailReg = /^([w-.]+@([w-]+.)+[w-]{2,4})?$/;
if(!emailReg.test(jQuery.trim($(this).val()))) {
var labelText = $(this).prev('label').text();
$(this).parent().append('<span class="error">You entered an invalid '+labelText+'.</span>');
hasError = true;
}
}
});
if(!hasError) {
$('form#contactForm li.buttons button').fadeOut('normal', function() {
$(this).parent().append('<img src="/wp-content/themes/td-v3/images/template/loading.gif" alt="Loading…" height="31" width="31" />');
});
var formInput = $(this).serialize();
$.post($(this).attr('action'),formInput, function(data){
$('form#contactForm').slideUp("fast", function() {
$(this).before('<p class="thanks"><strong>Thanks!</strong> Your email was successfully sent. I check my email all the time, so I should be in touch soon.</p>');
});
});
}

return false;

});
});



Just include it in the head of your document after jQuery, and you are good to go:
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/scripts/jquery.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/scripts/contact-form.js"></script>



Note: You may need to change the source depending upon where you upload the files in your theme.
Conclusion


View Demo Download Source Files


That’s it! Just upload the files and you’ve got your form. Since you’ve got the code right in front of you, it should be very easy to tweak and add on to. Enjoy!

Tạo form liên hệ trong wordpress không dùng plugin


How to Create a WordPress Contact Form Without a Plugin

There are a lot of different plugins available to put a contact form on your WordPress site, but I like to use as few plugins as i can get away with. So if you feel the same way, here’s a simple way to add a contact form, with some simple cut & paste of your site’s existing page.php file.
First step – Create your page template

Copy the code from your page.php file into a new file and name it page-contact.php.

In order to let WordPress know this file should be treated as a page template, we need to add a comment at the beginning of this new file:

<?php
/*
Template Name: Contact
*/
?>


The page-contact.php file should now look something like this:
<?php
/*
Template Name: Contact
*/
?>

<?php get_header() ?>

<div id="container">

<div id="content">
<?php the_post() ?>
<div id="post-<?php the_ID() ?>">
<div>
</div><!-- .entry-content ->
</div><!-- .post-->
</div><!-- #content -->
</div><!-- #container -->

<?php get_sidebar() ?>

<?php get_footer() ?>



Build the Form

Now you’ll need to create a simple contact form. This is very basic, allowing only name, email and comment fields. It should be pasted into your page-contact.php file, within the entry-content div.

<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
<ul>
<li>
<label for="contactName">Name:</label>
<input type="text" name="contactName" id="contactName" value="" />
</li>
<li>
<label for="email">Email</label>
<input type="text" name="email" id="email" value="" />
</li>
<li>
<label for="commentsText">Message:</label>
<textarea name="comments" id="commentsText" rows="20" cols="30"></textarea>
</li>
<li>
<button type="submit">Send email</button>
</li>
</ul>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>



Processing and Error Handling
Your form is taking shape now, but you still need it to verify submission and that required fields have been filled. When that is verified, it will get the admin’s email address and send the form to that address. if there’s a required field left blank, an error will be displayed.

Paste this code just before the get_header() function:

<?php
if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === '') {
$nameError = 'Please enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}

if(trim($_POST['email']) === '') {

$emailError = 'Please enter your email address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}

if(trim($_POST['comments']) === '') {

$commentError = 'Please enter a message.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}

if(!isset($hasError)) {

$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = '[PHP Snippets] From '.$name;
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

wp_mail($emailTo, $subject, $body, $headers);

$emailSent = true;
}

} ?>



Now, you’ll get an error message beneath the applicable field of the form if an email address is incorrect or a required field is left blank (such as “Invalid email address”, or “Please enter your name”). Your entire page-contact.php file should now look like this:

<?php
/*
Template Name: Contact
*/
?>

<?php

if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === '') {
$nameError = 'Please enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}

if(trim($_POST['email']) === '') {

$emailError = 'Please enter your email address.';
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}

if(trim($_POST['comments']) === '') {

$commentError = 'Please enter a message.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}

if(!isset($hasError)) {

$emailTo = get_option('tz_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = '[PHP Snippets] From '.$name;
$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;

wp_mail($emailTo, $subject, $body, $headers);

$emailSent = true;
}

} ?>

<?php get_header(); ?>
<div id="container">
<div id="content">

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<div>
<?php if(isset($emailSent) && $emailSent == true) { ?>
<div>
<p>Thanks, your email was sent successfully.</p>
</div>
<?php } else { ?>
<?php the_content(); ?>
<?php if(isset($hasError) || isset($captchaError)) { ?>
<p>Sorry, an error occured.<p>
<?php } ?>

<form action="<?php the_permalink(); ?>" id="contactForm" method="post">

<ul>
<li>
<label for="contactName">Name:</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" />
<?php if($nameError != '') { ?>
<span><?=$nameError;?></span>
<?php } ?>
</li>

<li>

<label for="email">Email</label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" />
<?php if($emailError != '') { ?>
<span><?=$emailError;?></span>
<?php } ?>
</li>

<li><label for="commentsText">Message:</label>

<textarea name="comments" id="commentsText" rows="20" cols="30"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
<?php if($commentError != '') { ?>
<span><?=$commentError;?></span>
<?php } ?>
</li>

<li>

<input type="submit">Send email</input>
</li>
</ul>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
<?php } ?>
</div><!-- .entry-content -->
</div><!-- .post -->

<?php endwhile; endif; ?>

</div><!-- #content -->
</div><!-- #container -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

If you have the coding skills, you can customize that contact form as you see fit, adding fields or exporting to a database. But this is a good start, and you’ll have eliminated one more plugin.

10/28/13

Giới thiệu 1 số hàm kiểm tra trong wordpress