Created
September 9, 2011 23:32
-
-
Save batuhan/1207604 to your computer and use it in GitHub Desktop.
simple contact form shortcode (for wp)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Simple Contact Form (Shortcode) | |
Plugin URI: http://beyn.org/ | |
Description: A simple contact form. Usage: [b_form email="send@form.here"] | |
Version: 0.1.1 | |
Author: Barış Ünver | |
Author URI: http://beyn.org/ | |
*/ | |
function get_real_ip() { | |
if ( isset($_SERVER) ){ | |
if ( isset($_SERVER['HTTP_X_FORWARDED_FOR']) ){ | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
}elseif ( isset($_SERVER['HTTP_CLIENT_IP']) ){ | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
}else{ | |
$ip = $_SERVER['REMOTE_ADDR']; | |
} | |
}else{ | |
if ( getenv('HTTP_X_FORWARDED_FOR') ){ | |
$ip = getenv('HTTP_X_FORWARDED_FOR'); | |
} | |
elseif ( getenv('HTTP_CLIENT_IP') ){ | |
$ip = getenv('HTTP_CLIENT_IP'); | |
}else{ | |
$ip = getenv('REMOTE_ADDR'); | |
} | |
} | |
return $ip; | |
} | |
function contact_form($atts) { | |
extract(shortcode_atts(array( | |
"eposta" => '', | |
"konu" => '' | |
), $atts)); | |
$ip_adresi = get_real_ip(); | |
$req_fields = array("ifoisim", "ifoeposta", "ifomesaj", "ifokonu"); | |
if (isset($_POST['ifogonder']) && trim($_POST['ifogonder']) != "") { | |
function has_newlines($text) { return preg_match("/(%0A|%0D|\n+|\r+)/i", $text); } | |
function has_emailheaders($text) { return preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i", $text); } | |
function is_valid($text) { return preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $text); } | |
foreach ($_POST AS $key => $value) { | |
if (get_magic_quotes_gpc()) { $value = stripslashes($value); } | |
$form_verisi[$key] = strip_tags($value); | |
} | |
$formerror = FALSE; | |
foreach ($req_fields AS $formlabel) { | |
$value = trim($form_verisi[$formlabel]); | |
if (empty($value)) { $formerror = TRUE; } | |
} | |
if (!$formerror) { | |
$error_msg = ""; | |
if (empty($error_msg)) { | |
$eposta_metni = $form_verisi['ifomesaj']; | |
$eposta_metni .= "\n\nIP: ". $ip_adresi; | |
$headers = "From: ".$form_verisi['ifoisim']." <".$form_verisi['ifoeposta'].">\n"; | |
$headers .= "Mime-Version: 1.0\n"; | |
$headers .= "Content-Type: text/plain; charset=UTF-8\n"; | |
$headers .= "Reply-To: ".$form_verisi['ifoisim']." <".$form_verisi['ifoeposta'].">\n"; | |
$headers .= "Organization: Beyn.org iletisim formu\n"; | |
$headers .= "Content-Transfer-Encoding: 8bit\n"; | |
$headers .= "Return-Path: ".$form_verisi['ifoisim']." <".$form_verisi['ifoeposta'].">\n"; | |
$headers .= "Errors-To: ".$eposta; | |
$eposta_konusu = "[Beyn] " . $form_verisi['ifokonu']; | |
if(!preg_match("/\[url/",$eposta_metni)) {mail($eposta, $eposta_konusu, $eposta_metni, $headers);} | |
$error_msg = "<div style=\"text-align:center;font-size:20px;color:#080;\">Mesajınız için teşekkürler!</div>\n"; | |
} | |
} else { $error_msg = "Lütfen tüm alanları doldurun.\n"; } | |
} | |
function get_value($formvalue) { | |
if (!empty($_POST[$formvalue])) { | |
if (get_magic_quotes_gpc()) { $form_value = stripslashes($_POST[$formvalue]);} else { $form_value = $_POST[$formvalue]; } | |
} | |
return $form_value; | |
} | |
$ifoisim_girilen = get_value('ifoisim'); | |
$ifoeposta_girilen = get_value('ifoeposta'); | |
$ifokonu_girilen = get_value('ifokonu'); | |
if (!empty($error_msg)) { $mesaj_cikti = "\n<div class=\"ifohata\">\n" .$error_msg. "</div>\n"; } | |
$cikti = ' | |
<style type="text/css"> | |
form.iletisim-formu {margin-top:15px;} | |
form.iletisim-formu div {clear:both;margin-bottom:10px;width:500px;} | |
form.iletisim-formu div label {width:200px;float:left;text-align:right;margin-right:5px;} | |
form.iletisim-formu div.ifohata {color:#c00;font-weight:bold;} | |
form.iletisim-formu #ifogonder {margin-top:5px;padding:0 10px;} | |
</style> | |
<form class="iletisim-formu" method="post">' . $mesaj_cikti . ' | |
<div> | |
<label for="ifoisim">Adınız:</label> | |
<input type="text" name="ifoisim" id="ifoisim" size="30" maxlength="50" value="' . $ifoisim_girilen . '" /> | |
</div> | |
<div> | |
<label for="ifoeposta">E-posta adresiniz:</label> | |
<input type="text" name="ifoeposta" id="ifoeposta" size="30" maxlength="50" value="' . $ifoeposta_girilen . '" /> | |
</div> | |
<div> | |
<label for="ifokonu">E-posta başlığı:</label> | |
<input type="text" name="ifokonu" id="ifokonu" size="30" maxlength="50" value="' . $konu . $ifokonu_girilen . '" /> | |
</div> | |
<div> | |
<label for="ifomesaj">Mesajınız:</label> | |
<textarea name="ifomesaj" id="ifomesaj" cols="30" rows="8">' . $ifomesaj_girilen . '</textarea><br /> | |
<label> </label> | |
<input type="submit" value="Gönder" name="ifogonder" id="ifogonder" /> | |
</div> | |
</form>'; | |
return $cikti; | |
} | |
add_shortcode('b_form', 'contact_form'); | |
/* Kaynak: http://www.stuffbysarah.net/2008/03/19/secure-php-contact-form/ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment