-- Select Count-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:38:23 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com