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表。非常感谢您对我的支持,谢谢。
答案 0 :(得分:3)
因为您应该返回表单输出以将其设置在确切位置,如果使用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
希望现在你明白了。
答案 1 :(得分:0)
使用
解决<?php
function form_creation()
{
global $wpdb;
ob_start();
?>
<form
然后
</form>
<?php return ob_get_clean(); } ?>