在WordPress短代码中使用PHP Echo

3

我正在为WordPress构建一组表单短代码。我已经放弃了将表单处理脚本放入短代码中的尝试,而是打算将其提供给所有页面使用。

然而,有一个回显会输出表单的响应,我想将其放入自己的短代码中。

<?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开发者,可能你已经注意到了。

3个回答

2
my_contact_form_generate_response函数中,在函数结尾的闭合}之前,您可以使用return $response;。然后,您可以执行echo my_contact_form_generate_response($type, $message);,它将回显return值,这种情况下将是$response

谢谢,但我仍然不确定如何使回声在函数内工作,以便我可以将其作为短代码调用。 - R Reveley

0

尝试进行更改

else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);

else if ($_POST['submitted']){
    $response = 'Some things';
    $response .= my_contact_form_generate_response("error", $missing_content);
    return $response;
}

我尝试过这个,但没有做短代码部分,但它没有输出任何内容。而且,如果在由短代码拉取的函数内部使用,这仍然不起作用吗? - R Reveley
所以要翻译...... else if ($_POST['submitted']){ $response = '一些东西'; echo $response; my_contact_form_generate_response("error", $missing_content); } ?>然后是短代码函数function form_response(){ return $response; }我错过了什么,因为现在表单部分下面的页面是空白的。另外,我不明白为什么要将$response设置为随机文本字符串? - R Reveley
@TheReveller http://pastebin.com/g50TR7nc 这是你可以使用的。抱歉回复晚了,哈哈。 - Quynh Nguyen

0

我不得不从全局调用 $response

function form_response(){
    echo $GLOBALS["response"];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接