duandu1915 2013-05-29 18:24 采纳率: 100%
浏览 89
已采纳

将自定义PHP添加到Wordpress页面

I have a small name generator script I am trying to add to my Wordpress website. The script itself works fine, I just don't know how to display it correctly.

It's a simple select form - the user selects a character race and a number of names to generate, and the script pulls from various text files and spits out the names. (Here's a live demo: http://muthaoithcreations.com/name2.php)

<?php

if(isset($_POST['submit']) && !empty($_POST['race']) && !empty($_POST['number'])) // after submitting and all fields are filled, this gets ran and the form below disappears
{
  $race = $_POST['race'];
  $number = $_POST['number'];

  echo "<p>You asked for $number $race name(s):</p>";

$first = explode("
", file_get_contents($race.'first.txt'));
$middle = explode ("
", file_get_contents($race.'middle.txt'));
$last = explode("
", file_get_contents($race.'last.txt'));
$first2 = explode("
", file_get_contents($race.'first.txt'));
$last2 = explode("
", file_get_contents($race.'last.txt'));

shuffle($first);
shuffle($middle);
shuffle($last);
shuffle($first2);
shuffle($last2);

if ($race == "Horc") { 
    for ($i = 0; $i < $number; $i++) {
    echo $first[$i] . $last[$i] . ' ' . $first2[$i] . $last2[$i] . "<br />
"; }
} elseif ($race == "Tiznt") {
    for ($i = 0; $i < $number; $i++) {  
    echo $first[$i] . $middle[$i] . $last[$i] . "<br />
"; }
} else {    
    for ($i = 0; $i < $number; $i++) {  
    echo $first[$i] . $last[$i] . "<br />
"; }
}

echo '<p><input type="button" onclick="history.back();" value="Go Back and Try Again!"></p>';

}

else // when the page loads, this else clause gets ran first
{

  echo '<p style="color:red;">Please choose your options.</p>';
?>
<form action="name2.php" method="post">
<label for="race">Select Charater Race:</label>
<select name="race" id="race" />
<option value="Oofo" selected>Oofo</option>
<option value="Tiznt">Tizn't</option>
<option value="Werm">Werm</option>
<option value="Horc">Horc</option>
</select>
<label for="number">Names to Generate:</label>
<select name="number" id="number" />
<option value="1" selected>1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
</select>
<input type="submit" name="submit" value="Give Me Names!"/>
</form>
<?php
}
?>

If I put the generator code into a Page Template and then create a new page using the template, the form displays correctly, but when the user submits the form:

Fatal error: Call to undefined function get_header() in /home/content/09/10184409/html/namegen-page.php on line 7

(The file namegen-page.php is my template file, which is in my theme directory... it appears it's trying to find it in my root directory.)

Do I need to have the form on a different page than the generator? Do I need to use a Wordpress plugin to display the PHP? I'm fairly new to Wordpress, and I'm not sure what I'm doing wrong. Any help is appreciated!

  • 写回答

3条回答 默认 最新

  • dongou2019 2013-05-30 09:19
    关注

    The problem is that you're setting the action for your form directly to your template file (using a relative URL, which would be unlikely to work in any case), and that doesn't initialize WordPress properly. Replace it with the (absolute) permalink to the page that uses the template.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器