0

Is there any way to write PHP code inside .twig file in WordPress? I am trying to send a mail by using the form values from a .twig template file.

page-pledgeform.twig

    {% extends "base.twig" %}

            {% block content %}
            <body class="{{body_class}}">
            <div class="wrapper">
            <section id="content" role="main" class="content-wrapper">
            {% block body %}
            <!-- strat_body_content -->
            <div class="body_content">
            <div class="space_height"></div>
            <div class="form_section">
            <div class="container">
            <div class="row">
            <div class="col-sm-8 col-sm-offset-2">  
            <!-- Top Navigation -->
            <div class="main clearfix">
            <form id="nl-form" method="POST" action="" class="nl-form">
            I am <input type="text" name="name" id="user_name" value="" placeholder="Name" data-subline="For example: <em>Mr. K. Roy</em>"/>
            It is a long  <input type="text" value="" placeholder="Country name" data-subline="For example: 
            <em>India</em>"/>
            Gender 
            <select>
            <option value="1" selected>Male</option>
            <option value="2">Female</option>
            <option value="3">Other</option>
            </select>
            Lorem
            <select>
            <option value="1" selected>O</option>
            <option value="2">B</option>
            <option value="3">B+</option>
            <option value="4">B-</option>
            <option value="2">A-</option>
            </select>
            .
            There are many  <input type="text" value="" placeholder="Address" data-subline="For example: <em>Los Angeles</em> or <em>New York</em>"/>
            passages of
            <select>
            <option value="1" selected>Mumbai</option>
            <option value="2">Delhi</option>
            <option value="3">Bangalore</option>
            <option value="4">Hyderabad</option>
            <option value="5">Ahmedabad</option>
            <option value="6">Kolkata</option>
            </select>
            classical Latin
            <select>
            <option value="1" selected>Gujarat</option>
            <option value="2">Andhra Pradesh</option>
            <option value="3">Tamil Nadu</option>
            <option value="4">Maharashtra</option>
            <option value="5">West Bengal</option>
            </select>
            first true
            <select>
            <option value="1" selected>Vijayawada</option>
            <option value="2">Kanyakumari</option>
            <option value="3">Rewalsar</option>
            <option value="4">Dehradun</option>
            <option value="2">24 parganas</option>
            </select>
            There are many  <input type="text" value="" placeholder="Pin code" data-subline="For example: <em>700049</em>"/>
            There are  <input type="email" value="" placeholder="Email ID" data-subline="For example: <em>mac@gmail.com</em>"/>
            Phone  <input type="number" value="" placeholder="Phone Number" data-subline="For example: <em>000-123-000</em>"/>
            Mobile number  <input type="number" value="" placeholder="Mobile Number" data-subline="For example: <em>000-123-000</em>"/>
            My information may be shared with the
            <select>
            <option value="1" selected>Yes</option>
            <option value="2">No</option>
            </select>
            <div class="nl-overlay"></div>
            <br><br><br>
            <h4>Emergency Contact Person Details :-</h4>
            I am <input type="text" name="name" value="" placeholder="Name" data-subline="For example: <em>Mr. k. Roy</em>"/>
            It is a long  <input type="text" value="" placeholder="Country name" data-subline="For example: <em>India</em>"/>
            Gender 
            <select>
            <option value="1" selected>Male</option>
            <option value="2">Female</option>
            <option value="3">Other</option>
            </select>
            Lorem
            <select>
            <option value="1" selected>O</option>
            <option value="2">B</option>
            <option value="3">B+</option>
            <option value="4">B-</option>
            <option value="2">A-</option>
            </select>
            .
            There are many  <input type="text" value="" placeholder="Address" data-subline="For example: <em>India</em>"/>
            passages of
            <select>
            <option value="1" selected>Mumbai</option>
            <option value="2">Delhi</option>
            <option value="3">Bangalore</option>
            <option value="4">Hyderabad</option>
            <option value="5">Ahmedabad</option>
            <option value="6">Kolkata</option>
            </select>
            classical Latin
            <select>
            <option value="1" selected>Gujarat</option>
            <option value="2">Andhra Pradesh</option>
            <option value="3">Tamil Nadu</option>
            <option value="4">Maharashtra</option>
            <option value="5">West Bengal</option>
            </select>
            first true
            <select>
            <option value="1" selected>Vijayawada</option>
            <option value="2">Kanyakumari</option>
            <option value="3">Rewalsar</option>
            <option value="4">Dehradun</option>
            <option value="2">24 parganas</option>
            </select>
            There are many  <input type="text" value="" placeholder="Pin code" data-subline="For example: <em>700049</em>"/>
            There are  <input type="email" value="" placeholder="Email ID" data-subline="For example: <em>mac@gmail.com</em>"/>
            Phone  <input type="number" value="" placeholder="Phone Number" data-subline="For example: <em>000-123-000</em>"/>
            Mobile number  <input type="number" value="" placeholder="Mobile Number" data-subline="For example: <em>000-123-000</em>"/>
            My information may be shared with the
            <select>
            <option value="1" selected>Yes</option>
            <option value="2">No</option>
            </select>
            <div class="clearfix"></div>
            <div class="nl-submit-wrap" style="float: right;">
            <button class="nl-submit" name="submit" id="btnSubmit" type="submit">Agree & Submit</button>
            </div>
            <div class="clearfix"></div>
            <div class="nl-overlay"></div>
            </form>
            </div>
            </div>
            </div>
            </div>
            </div>
            </div>
            {% endblock %}
            {% block extrajs %}
            <script src="{{theme.link}}/js/nlform.js"></script>
            <script>
            var nlform = new NLForm( document.getElementById( 'nl-form' ) );
            </script>
            <script>
            var emargency = new NLForm( document.getElementById( 'emargency' ) );
            </script>
            {% endblock %}

            </div>
            </body>
            </html>
            {% endblock %}
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • 1
    You can't write code directly in a twig file. You need to create twig functions for that. Also, you shouldn't send emails from the templates. Let the templates handle the output of the site. Look into using Javascript and Ajax to post the form and set up a function in your functions.php-file that sends the email instead. – M. Eriksson Dec 04 '18 at 06:52

1 Answers1

1

twig is not like blade, you can't write php code inside. However, you can write your own twig extensions:

https://symfony.com/doc/current/templating/twig_extension.html

please be sure to separate your twig extension from your logic (send mail), add this functionality on a service and bridge both (service & twig extension).

treyBake
  • 6,440
  • 6
  • 26
  • 57
MoxGeek
  • 458
  • 6
  • 17