php - WordPress 插件短代码始终显示在顶部

标签 php jquery wordpress

WordPress 插件短代码始终显示在顶部。 看看下面的代码

<?php
/*
Plugin Name: MyPlugin
Description: Test
Author: Bassem
License: GPL
*/
?>
<?php 
function  form_creation()
{
    global $wpdb;
?>
<form action=" <?php get_permalink(); ?> " method="post" id="myform">
<table>

<tr>
<td><input type="text" id="txtname" name="txtname" placeholder="Your Name"/> </td>
</tr>

<tr>
<td><input type="email" id="txtemail" name="txtemail" placeholder="Your Email Address "  /> </td>
</tr>

<tr>
<td>
<!-- drop down menu  (Country )-->
<select id='select_Country' name="select_Country" >
        <option selected="selected" disabled="disabled"> -- Select Country -- </option>
        <?php
        $query='select Code,Country from _country order by Country';
        $result = $wpdb->get_results($query);
        foreach( $result as $row ) 
        {
           echo '<option value='.$row->Code.'>'.$row->Country.'</option>';
        }
        ?>
</select> 
</td>
</tr>


<tr>
<td>
<!-- drop down menu  (City )-->
<select id="select_city" name="select_city">
    <option selected="selected"> -- Select City -- </option>
</select>
</td>
</tr>

<tr>
<td> <input type="submit" id="btnsubmit" value="Submit" name='submit'/> </td>
</tr>

</table>
</form>
<?php } ?>
<?php
if($_POST['submit'])
{
    $name=strip_tags($_POST['txtname']);
    $email=strip_tags($_POST['txtemail']);
    $country=$_POST['select_Country'];
    $city=$_POST['select_city'];
    $insertQuery="insert into _customers(Name,Email,Country,City)values('$name','$email', '$country','$city')";
    $wpdb->query($insertQuery);
    if($wpdb)
    {
        echo 'Data Inserted Successfully';
    }
}
?>
<?php add_shortcode('myshortcode',form_creation); ?>
<?php function my_scripts_method()
    {
        wp_enqueue_script('jquery'); //add jQuery Library 
        wp_enqueue_script('fn',plugins_url('/js/ajaxfn.js' , __FILE__ ),array( 'jquery' )); //ajax operations
        wp_enqueue_script('jquery.validate.min',plugins_url('/js/jquery.validate.min.js' , __FILE__ ),array( 'jquery' )); //Validation

    }
    add_action( 'wp_enqueue_scripts', 'my_scripts_method' ); //execute the function [ my_scripts_method ]
    ?>

上面的代码工作正常,但它有一个问题,当我将 [myshortcode] 放在特定页面或帖子中时,它总是显示在顶部,无论它在哪里,我搜索并发现我没有回显表格但返回它,但我不知道如何使用 wp 插件功能返回 html 表。非常感谢您对我的支持,谢谢。

最佳答案

因为您应该返回表单输出以将其设置在确切的位置,如果您使用 echo,它将始终显示在顶部。

解决方案是将表单存储在变量中然后返回它,

$return = '<form>........</form>';
return $return;

更新:

在 HTML 和 PHP LOGIC 中单独划分,看看这个例子:

function my_shortcode() {

    // Set HTML saperate and return it
    return include('template/shortcode_template.php');
}

shortcode_template.php

<?php ob_start(); ?>
    <strong>Hi there!</strong>
    <p>
        This is an include test for shortcodes...
    </p>
<?php return ob_get_clean();

结构:

- Plugin_dir
- Plugin_dir/plugin.php // Holds your shortcode logic
- Plugin_dir/template/shortcode_template.php  // Holds HTML PART

希望现在你明白了。

关于php - WordPress 插件短代码始终显示在顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27395719/

相关文章:

javascript - 如何防止事件引发?

javascript - 如何检测请求是否被中止?

php - 将 HTML 模板与 Wordpress PHP 文件混合

php - 两个同时 AJAX 请求不会并行运行

javascript - Google Analytics 电子商务跟踪数据问题

PHP DOM : Get all text values

jQuery 元素无法选择parent()

wordpress - WooCommerce 按价格订购无法正常工作

PHP/SSH 正则表达式脚本/命令,用于从多个文件中删除相同的恶意软件代码

php - 正则表达式捕获 : repeating two letters