创建一个html表单的短代码

时间:2018-01-06 10:03:03

标签: php wordpress

我正在尝试为html表单创建一个简短的代码。因为我想在我的单词页面中多次使用这个页面 我不知道该怎么做。如果有任何帮助请帮帮我

<form action="<?php echo get_page_link(2599) ?>" method="POST" >
                  <input type='hidden' name='page_id' value='2599'>
                    <div class="row">
                        <div class="form-group col-sm-3 col-md-2" id="bgform_text2">
                            <h4>SEARCH</h4>
                            <p>For Your Favourite Place</p>
                        </div>
                        <div class="form-group col-sm-5 col-md-2">
                            <label>Where to ? </label>
                            <input  type="text" name="names" class="input-text full-width" placeholder="start typing here....">
                        </div>
                        <div class="form-group col-sm-5 col-md-4">
                            <div class="row">
                                <div class="col-xs-6">
                                    <label>Arrive </label>
                                    <div class="controls">
                                        <input type="text" name="arive_time" id="departing">
                                    </div>
                                </div>
                                <div class="col-xs-6">
                                    <label>Departs </label>
                                    <div class="controls">
                                        <input type="text" name="depart_time" id="returning">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group col-sm-9 col-md-2">
                            <label>Sleeps </label>
                            <div class="controls">
                                <i class="fa fa-sort"></i>
                                                            <select name="sleeps">
                              <option value="" disabled="" selected=""></option>
                              <option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></option>
                            </select>

                          </div> 

                        </div>
                        <div class="form-group col-sm-3 col-md-2">
                            <input type="submit" value="submit" name="submit_btn" class="button">
                        </div>
                    </div>
                </form>

2 个答案:

答案 0 :(得分:1)

试试这个。

<?php 

    function my_shortcode(){

        $pagelink = get_page_link(2599);

        return '<form action="'.$pagelink.'" method="POST" >
              <input type="hidden" name="page_id" value="2599">
                <div class="row">
                <div class="form-group col-sm-3 col-md-2" id="bgform_text2">
                    <h4>SEARCH</h4>
                    <p>For Your Favourite Place</p>
                </div>
                <div class="form-group col-sm-5 col-md-2">
                    <label>Where to ? </label>
                    <input  type="text" name="names" class="input-text full-width" placeholder="start typing here....">
                </div>
                <div class="form-group col-sm-5 col-md-4">
                    <div class="row">
                    <div class="col-xs-6">
                        <label>Arrive </label>
                        <div class="controls">
                        <input type="text" name="arive_time" id="departing">
                        </div>
                    </div>
                    <div class="col-xs-6">
                        <label>Departs </label>
                        <div class="controls">
                        <input type="text" name="depart_time" id="returning">
                        </div>
                    </div>
                    </div>
                </div>
                <div class="form-group col-sm-9 col-md-2">
                    <label>Sleeps </label>
                    <div class="controls">
                    <i class="fa fa-sort"></i>
                                    <select name="sleeps">
                      <option value="" disabled="" selected=""></option>
                      <option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option></option>
                    </select>

                  </div> 

                </div>
                <div class="form-group col-sm-3 col-md-2">
                    <input type="submit" value="submit" name="submit_btn" class="button">
                </div>
                </div>
            </form>';

     }
    add_shortcode('my_shortcode_name','my_shortcode');
    ?>

您可以在页面中使用此短代码

<?php echo do_shortcode('[my_shortcode_name]');?>

答案 1 :(得分:0)

我想你是说你不想在项目的多个地方复制粘贴完全相同的代码PHP has an include control structure method来获取任何文件并在运行时使用这些内容。

您的代码最终可能如下所示:

# form definition inside the main page that needs the form
<form action="<?php echo get_page_link(2599) ?>" method="POST" >
    # include the template
    <?php include('template.php') ?>
</form>

# template.php contains the html form
<input type='hidden' name='page_id' value='2599'>