gpt4 book ai didi

WordPress 短代码中的 PHP Echo

转载 作者:搜寻专家 更新时间:2023-10-31 21:02:51 24 4
gpt4 key购买 nike

我正在 Wordpress 中构建一组表单简码。我已经放弃尝试将表单处理脚本转换为简码,而只是让所有页面都可以使用它。

但是有一个 echo 输出我想放入它​​自己的短代码中的表单的响应。

<?php echo $response; ?>

这需要放在表单的顶部,以便验证消息出现在正确的位置。

总表单处理代码:

<?php

//response generation function

$response = "";

//function to generate response
function my_contact_form_generate_response($type, $message){

global $response;

if($type == "success") $response = "<div class='callout success'><p>{$message}</p></div>";
else $response = "<div class='callout error'><p>{$message}</p></div>";
}

// response messages
$not_human = "Please fill out the human verification field to prove you are not a spam robot. A number 2 will do the job nicely.";
$missing_content = "It looks like we are missing something. Please chek the form below.";
$email_invalid = "That email Address doesn't look quite right.";
$message_unsent = "Your message wasn't sent. Please have another go. If it still doesn't work then please call us using the number supplied.";
$message_sent = "Thank you for your enquiry. We will be in contact shortly.";

// user posted variables
$business_name = $_POST['message_business_name'];
$first_name = $_POST['message_first_name'];
$last_name = $_POST['message_last_name'];
$email = $_POST['message_email'];
$phone = $_POST['message_phone'];
$human = $_POST['message_human'];
$location = $_POST['location'];
$message_opt_in = $_POST['message_opt_in'];

$optin = "";
if ($message_opt_in == "on"){
$optin = "The user has opted in to marketing emails";
}
else {
$optin = "!!!!!!!!!!!!!!!!!!!!!!\n\n The user has NOT opted in to marketing emails! \n\n!!!!!!!!!!!!!!!!!!!!!!\n\n";
}

$body = "$optin \n\n\n\n This message was sent from xxx.com $location\n\nBusiness name: $business_name \nName: $first_name \nName: $last_name \nEmail: $email \nPhone: $phone";

//php mailer variables
$to ="xxx@xxx.com";
$subject = "Someone sent a message from ".get_bloginfo('name');
$headers = 'From: '. $email . "\r\n" .
'Reply-To: ' . $email . "\r\n";
if(!$human == 0){
if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
else {

//validate email
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
my_contact_form_generate_response("error", $email_invalid);
else //email is valid
{
//validate presence of name and message
if(empty($business_name) || empty($first_name) || empty($email)){
my_contact_form_generate_response("error", $missing_content);
}
else //ready to go!
{
$sent = wp_mail($to, $subject, strip_tags($body), $headers);
if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
}
}
}
}
else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);

?>
<?php echo $response; ?>

我尝试过其他可能会让 PHP 开发人员发笑的事情。

function form_response(){
echo $response;
}

function form_response(){
echo '<?php echo "$response"; ?>';
}

免责声明我不是 PHP 开发人员,您可能已经看出来了。

最佳答案

my_contact_form_generate_response功能,你可以做到return $response;在收盘前的底部}为函数。

然后,你可以做 echo my_contact_form_generate_response($type, $message);它会回应 return值,将为 $response在这种情况下。

关于WordPress 短代码中的 PHP Echo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38499654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com