: How to add custom php code in Wordpress text widget I cant make my PHP code work when I add it in WordPress text widget (without plugin). The same code works on main (index.php) , but not
I cant make my PHP code work when I add it in WordPress text widget (without plugin). The same code works on main (index.php) , but not in widget. I searched for this question and found the answer - which says me to add the code in the end of the functions.php file, so I did but afterwards on the top of the header I mentioned the text I've just pasted in function file.
EDIT: adding the code
<?php
error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP
//If the form is submitted
if(isset($_POST['submitted'])) {
// require a name from user
if(trim($_POST['contactName']) === '') {
$ContactNameError = 'Forgot your name!';
$hasError = true;
} else {
$ContactName = trim($_POST['contactName']);
}
// need valid email
if(trim($_POST['email']) === '') {
$emailError = 'Forgot to enter in your e-mail 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']);
}
/*
// we need at least some content
if(trim($_POST['comments']) === '') {
$commentError = 'You forgot to enter a message!';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}*/
if(trim($_POST['phone']) === '') {
$phoneError = 'Forgot your number!';
$hasError = true;
} else {
$phone = trim($_POST['phone']);
if(function_exists('stripslashes')) {
$phone = stripslashes(trim($_POST['phone']));
} else {
$phone = trim($_POST['phone']);
}
}
// upon no failure errors let's email now!
if(!isset($hasError)) {
$emailTo = 'litehacker@mail.ru, georgiantours1@gmail.com, info@travel-georgia.co.il ' ;
$subject = $_POST['subject'];
$comments= $_POST['comments'];
$dge= $_POST['dge'];
$tve= $_POST['tve'];
$weli= $_POST['weli'];
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $ContactName nnEmail: $email nnComments: $comments nn Subject :$subject nn Number: $phone nn $dge / $tve /$weli";
mail($emailTo, ": הודעה" , $body, "Travel-Georgia" );
/////////////////
// set our boolean completion value to TRUE
$emailSent = true;
}
}
?>
<!-- @begin contact -->
<div id="contact" class="section">
<div class="container content" style="padding: 3px;">
<?php if(isset($emailSent) && $emailSent == true) { ?>
<p class="info">פנייתכם התקבלה במערכת</p>
<?php } else { ?>
<div class="desc2">
<p style="font-size:28px; padding-bottom: 10px; ">לפרטים והזמנות<p>
<p style="font-size:28px; padding-bottom: 10px; ">0722-50-40-44<p>
</div>
<div id="contact-form">
<?php if(isset($hasError) || isset($captchaError) ) { ?>
<p class="alert">Error submitting the form</p>
<?php } ?>
<form id="contact-us" method="post" accept-charset="utf-8">
<div class="formblock">
<label class="screen-reader-text">שם</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="txt requiredField" placeholder="" />
<?php if($ContactNameError != '') { ?>
<br /><span class="error"><?php echo $ContactNameError;?></span>
<?php } ?>
</div>
<div class="formblock">
<label class="screen-reader-text">דוא"ל</label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="txt requiredField email" placeholder="" />
<?php if($emailError != '') { ?>
<br /><span class="error"><?php echo $emailError;?></span>
<?php } ?>
</div>
<div class="formblock">
<label class="screen-reader-text">מספר טלפון</label>
<input type="text" name="phone" value="<?php if(isset($_POST['phone'])) echo $_POST['phone'];?>"> <?php if($phoneError != '') { ?>
<br /><span class="error"><?php echo $phoneError;?></span>
<?php } ?>
</div>
<div class="formblock">
<label class="screen-reader-text">בחרו טיול</label>
</div>
<div class="styled-select">
<select name="subject" size="1" >
<option selected></option>
<option>מבצע 8 ימים (מאורגן ג'יפים 1449$)</option>
<option>מבצע 8 ימים (מאורגן מיניבוס 1259$)</option>
<option>מבצע 6 ימים (סטודנטים/צעירים 990$) </option>
<option>מאורגן 8 ימים (מיניבוס)</option>
<option>מאורגן 10 ימים (מיניבוס)</option>
<option>מאורגן 12 יום (מיניבוס)</option>
<option>טיולי ג'יפים 6 ימים (מאורגן)</option>
<option>טיולי ג'יפים 8 ימים (מאורגן)</option>
<option>טיולי ג'יפים 10 ימים (מאורגן)</option>
<option>טיול עצמאי 8 ימים</option>
<option>טיול עצמאי 10 ימים</option>
<option>טיול עצמאי 12 יום</option>
<option>טיולים פרטיים/משפחות</option>
</select>
<br />
</div>
<div class="formblock">
<label class="screen-reader-text">תאריך יציאה</label>
<ul class="form-ul">
<li class="form-li">
<select name="weli" size="1">
<option selected >שנה</option>
<option >2014</option>
<option >2015</option>
<option >2016</option>
<option >2017</option>
<option >2018</option>
</select>
</li>
<li class="form-li">
<select name="tve" size="1">
<option >חודש</option>
<option >01</option>
<option >02</option>
<option >03</option>
<option >04</option>
<option >05</option>
<option >06</option>
<option >07</option>
<option >08</option>
<option >09</option>
<option >10</option>
<option >11</option>
<option >12</option>
</select>
</li>
<li class="form-li">
<select name="dge" size="1">
<option selected >יום</option>
<option >01</option>
<option >02</option>
<option >03</option>
<option >04</option>
<option >05</option>
<option >06</option>
<option >07</option>
<option >08</option>
<option >09</option>
<option >10</option>
<option >11</option>
<option >12</option>
<option >13</option>
<option >14</option>
<option >15</option>
<option >16</option>
<option >17</option>
<option >18</option>
<option >19</option>
<option >20</option>
<option >21</option>
<option >22</option>
<option >23</option>
<option >24</option>
<option >25</option>
<option >26</option>
<option >27</option>
<option >28</option>
<option >29</option>
<option >30</option>
<option >31</option>
</select>
</li>
</ul>
</div>
<div class="formblock">
<label class="screen-reader-text">מכתב</label>
<textarea name="comments" id="commentsText" class="txtarea requiredField" row="6" ></textarea>
</div>
<button name="submit" type="submit" class="subbutton">שלח</button>
<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
</div>
<?php } ?>
</div>
</div>
<!-- End #contact -->
More posts by @Sarah324
1 Comments
Sorted by latest first Latest Oldest Best
You can't include php code in text widgets from the Wordpress admin panel by default.
You've added far too much code in your question (instead of just including the relevant snippets - use pastbin for full contents of files).
But you can add the ability to include php in text widgets by adding the following in your theme's functions.php file.
add_filter('widget_text', 'enable_php_code', 99);
function enable_php_code ($text) {
if (strpos($text, '<' . '?') !== false) {
ob_start();
eval('?' . '>' . $text);
$text = ob_get_contents();
ob_end_clean();
}
return $text;
}
Or try this function with explanation here:-
add_filter('widget_text','execute_php',99);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.