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 plugin函数返回html表。非常感谢您对我的支持,谢谢。

最佳答案

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

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

$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/

10-11 17:40