我试图为WordPress创建的PHP联系人表单有问题。我在$headers
上面写着syntax error, unexpected \'\'
但我不知道为什么,因为我觉得这是正确的。我也认为function my_form_message(){
未正确关闭。任何帮助都会很好。谢谢
// Form markup
function html_form_code() { ?>
<form action="<?php esc_url( $_SERVER[\'REQUEST_URI\'] ); ?>" method="post">
<p>Your Name (required)<br />
<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="<?php isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : \'\'; ?>" size="40" />
</p>
<p>Your Email (required)<br />
<input type="email" name="cf-email" value="<?php isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : \'\'; ?>" size="40" />
</p>
<p>Your Message (required)<br />
<textarea rows="10" cols="35" name="cf-message"><?php isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : \'\'; ?></textarea>
</p>
<p><input type="submit" name="cf-submitted" value="Send"/></p>
</form>
<?php }
// Form validation
function my_validate_form() {
$errors = new WP_Error();
if ( isset( $_POST[ \'content\' ] ) && $_POST[ \'content\' ] !== \'\' ) {
$errors->add( \'cheater\', \'Sorry, this field should not be filled. Are you trying to cheat?\' );
}
if ( isset( $_POST[ \'cf-name\' ] ) && $_POST[ \'cf-name\' ] == \'\' ) {
$errors->add(\'name_error\', \'Please fill in a valid name.\' );
}
if ( isset( $_POST[ \'cf-email\' ] ) && $_POST[ \'cf-email\' ] == \'\' ) {
$errors->add(\'email_error\', \'Please fill in a valid email.\' );
}
if ( isset( $_POST[ \'cf-message\' ] ) && $_POST[ \'cf-message\' ] == \'\' ) {
$errors->add(\'message_error\', \'Please fill in a valid message.\' );
}
return $errors;
}
// Form delivery
function deliver_mail( $args = array() ) {
// This $default array is a way to initialize some default values that will be overwritten by our $args array.
// We could add more keys as we see fit and it\'s a nice way to see what parameter we are using in our function.
// It will only be overwritten with the values of our $args array if the keys are present in $args.
// This uses WP wp_parse_args() function.
$defaults = array(
\'name\' => \'\',
\'email\' => \'\',
\'message\' => \'\',
\'to\' => get_option( \'admin_email\' ), // get the administrator\'s email address
);
$args = wp_parse_args( $args, $defaults );
$headers = "From: $args[\'name\'] <$args[\'email\']>" . "\\r\\n";
// Send email returns true on success, false otherwise
if( wp_mail( $args[\'to\'], $args[\'message\'], $headers ) ) {
return;
}
else {
return false;
}
}
// Form sanitize
function my_sanitize_field( $input ){
return trim( stripslashes( sanitize_text_field ( $input ) ) );
}
// Form succsess message
function my_form_message(){
global $errors;
if( is_wp_errors( $errors ) && empty( $errors->errors ) ){
echo \'<div class="cf-success">\';
echo \'<p>Thank you for contacting us \'. $_POST[\'cf-name\'] .\', a member of our team will be in touch with you shortly.</p>\';
echo \'</div>\';
//Empty $_POST because we already sent email
$_POST = \'\';
}
else {
if( is_wp_errors( $errors ) && ! empty( $errors->errors ) ){
$error_messages = $errors->get_error_messages();
foreach( $error_messages as $k => $message ){
echo \'<div class="cf-error \' . $k . \'">\';
echo \'<p>\' . $message . \'</p>\';
echo \'</div>\';
}
}
}
// Form shortcode
add_shortcode( \'contact_form\', \'cf_contact_form\' );
function cf_contact_form() {
ob_start();
my_form_message();
html_form_code();
return ob_get_clean();
}
// Error validation
add_action( \'init\', \'my_cf_form\');
function my_cf_form(){
if( isset( $_POST[\'cf-submitted\'] ) ) {
global $errors;
$errors = my_validate_form();
if( empty( $errors->errors ) ){
$args = array(
\'name\' => my_sanitize_field( $_POST[\'cf-name\'] ),
\'email\' => my_sanitize_field( $_POST[\'cf-email\'] ),
\'message\' => my_sanitize_field( $_POST[\'cf-message\'] ),
);
deliver_mail( $args );
}
else {
return $errors;
}
}
}