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条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?