Php 创建html表单的短代码

Php 创建html表单的短代码,php,wordpress,Php,Wordpress,我正在尝试为html表单创建一个简短的代码。因为我想在我的word新闻页面中多次使用此选项。 我不知道怎么做。如果需要帮忙,请让我帮忙 <form action="<?php echo get_page_link(2599) ?>" method="POST" > <input type='hidden' name='page_id' value='2599'> <div cla

我正在尝试为html表单创建一个简短的代码。因为我想在我的word新闻页面中多次使用此选项。
我不知道怎么做。如果需要帮忙,请让我帮忙

<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>
试试这个

<?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]');?>

我想你是说,你不想在项目中的多个地方复制粘贴完全相同的代码,不想在运行时获取任何文件并使用内容

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

# 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'> 
#需要表单的主页内的表单定义

vel@thanks支持,但当我试图使用此功能时,它不起作用。短代码不打印任何内容在文本小部件或页面中添加此短代码吗?vel@i我在pagescan中使用了这个,你可以分享我的屏幕截图吗?是的@你可以通过team viewer检查,我可以分享我的id吗