2016-07-21 12 views
3

私はWordpressでフォームショートコードのセットを構築しています。私はフォーム処理スクリプトを短いコードにしようとするのをやめ、すべてのページで使用できるようにするつもりです。PHP Echo in Wordpressショートコード

私は自分のショートコードに入れたいフォームのレスポンスを出します。

これは、検証メッセージが適切な場所に表示されるように、フォームの上部に移動する必要があります。

合計フォーム処理コード:

<?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 ="[email protected]"; 
    $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 devの笑いになるだろうとりわけ試してみました。

function form_response(){ 
    echo $response; 
} 

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

免責事項私は、あなたはおそらく言うことができるようにPHPの開発者ではありませんよ。

答えて

0

私はグローバル

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

my_contact_form_generate_response機能で

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

から$応答を呼び出すために持っていた私はショートビットを行うことなくこれを試してみましたが、それは何も出力にも、これはまだショートによって引っ張ら関数内で動作しませんしませんでしたか? –

+0

短いコードではリターンを使用する必要があります。エコーではない –

+0

翻訳するには ... else if($ _POST ['submitted']){ $ response = 'Some things'; echo $ response; my_contact_form_generate_response( "エラー"、$ missing_content); }> そしてショート機能 関数form_response(){ リターン$応答;? } フォーム部分の下にページが空白になっているので、何か不足しています。また、なぜランダムなテキストの文字列に$応答を設定するのか分からないのですか? –

2

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

を変更しようとすると、関数の終了}前の一番下にreturn $response;を行うことができます。

次に、echo my_contact_form_generate_response($type, $message);を実行すると、returnの値が返されます。この場合、$responseになります。

+0

ありがとうございますが、私は短コードとして引き出すことができる関数の中でエコーを動作させる方法はまだ分かりません。 –