对于PHP来说,我是一个完全新手,但是我最近一直试图将计算器应用程序重新创建为Wordpress插件,但是我已经失败了。
这是尝试将其转换为插件之前的原始代码。
<?php
if (isset($_POST['person1'])) $person1 = $_POST['person1'];
if (isset($_POST['person2'])) $person2 = $_POST['person2'];
$multivar = 4;
$answer = ($person1 + $person2) * $multivar;
echo <<<_END
<form method='post' action='index.php'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class="table">
<tr class="calcheading"><td colspan="2"><strong>How much can you borrow?</strong></td></tr>
<tr class="calcrow"><td>Person 1 income:</td><td align="center"><input type='text' name='person1' value="$person1"/></td></tr>
<tr class="calcrow2"><td>Person 2 income</td><td align="center"><input type='text' name='person2' value="$person2"/></td></tr>
<tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
_END;
?>
<tr class="calcrow">
<td><i>You can borrow up to:</td>
<td align="center"><input type="text" value="<?php echo round($answer)?>"></td></i>
</tr>
</table>
</form>
该代码本身就很好,它完全适用于我需要它工作的方式,但是当它在Wordpress页面中实现时失败了。我尝试创建一个页面模板并在其中包含该脚本,但没有运气(因为它是我为我制作的主题中的快速复杂代码)。所以我决定最好的方法是将它转换成插件然后用短代码将其转发到Wordpress页面上,所以我做了一些阅读并试图这样做:
<?php
/*
Plugin Name: calc
Plugin URI:
Description: Calculates loans/how much can be borrowed
Version: 1.0
Author: Mikey
Author URI:
*/
//WordPress Hooks
add_shortcode('bcalc', 'run_bc');
function run_bc()
{
if (isset($_POST['bc_person1'])) $bc_person1 = $_POST['bc_person1'];
if (isset($_POST['bc_person2'])) $bc_person2 = $_POST['bc_person2'];
$bc_multivar = 4;
$bc_answer = ($bc_person1 + $bc_person2) * $bc_multivar;
}
function bc_getForm()
{
echo "<form method='post' action='".get_permalink()."'>";
echo "<table border='0' width='500px' cellpadding='3' cellspacing='1' class='table'>";
echo "<tr class='calcheading'><td colspan="2"><strong>How much can you borrow?</strong></td></tr>";
echo "<tr class='calcrow'><td>Person 1 income:</td>";
echo "<td align='center'><input type='text' name='bc_person1' value='$bc_person1'/></td></tr>";
echo "<tr class='calcrow2'><td>Person 2 income</td><td align='center'><input type='text' name='bc_person2' value='$bc_person2'/></td></tr>";
echo "<tr class='submit'><td colspan="2"><input type='submit' value='Calculate'/></td></tr>";
echo "<tr class='calcrow'>";
echo "<td><i>You can borrow up to:</td>";
echo "<td align='center'><input type='text' value='.echo round($bc_answer)'></td></i>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
bc_getForm();
?>
我已经明白我的代码太可怕了,我也不是要求真正用勺子喂食,我只是需要某种形式的指导来帮助我找到正确的方法。
感谢您的时间
答案 0 :(得分:2)
这应该让你开始!
<?php
/*
Plugin Name: calc
Plugin URI:
Description: Calculates loans/how much can be borrowed
Version: 1.0
Author: Mikey
Author URI:
*/
//WordPress Hooks
add_shortcode('bcalc', 'run_bc');
function run_bc()
{
if (isset($_POST['bc_person1'])) $bc_person1 = $_POST['bc_person1'];
if (isset($_POST['bc_person2'])) $bc_person2 = $_POST['bc_person2'];
$bc_multivar = 4;
$bc_answer = ($bc_person1 + $bc_person2) * $bc_multivar;
?>
<form method='post' action='<?php echo get_permalink(); ?>'>
<table border='0' width='500px' cellpadding='3' cellspacing='1' class='table'>
<tr class='calcheading'><td colspan='2'><strong>How much can you borrow?</strong></td></tr>
<tr class='calcrow'><td>Person 1 income:</td>
<td align='center'><input type='text' name='bc_person1' value='<?php echo $bc_person1 ?>'/></td></tr>
<tr class='calcrow2'><td>Person 2 income</td><td align='center'><input type='text' name='bc_person2' value='<?php echo $bc_person2 ?>'/></td></tr>
<tr class='submit'><td colspan='2'><input type='submit' value='Calculate'/></td></tr>
<tr class='calcrow'>
<td><i>You can borrow up to:</td>
<td align='center'><input type='text' value='<?php echo round($bc_answer); ?>'></td></i>
</tr>
</table>
</form>
<?php
}