2013-05-29 227 views
0

我有一个小的名称生成器脚本,我试图添加到我的WordPress的网站。脚本本身工作正常,我只是不知道如何正确显示它。添加自定义PHP到Wordpress页面

这是一个简单的选择形式 - 用户选择一个角色竞赛和一些名称来生成,脚本从各种文本文件中提取并吐出名称。 (这里有一个现场演示: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("\n", file_get_contents($race.'first.txt')); 
$middle = explode ("\n", file_get_contents($race.'middle.txt')); 
$last = explode("\n", file_get_contents($race.'last.txt')); 
$first2 = explode("\n", file_get_contents($race.'first.txt')); 
$last2 = explode("\n", 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 />\n"; } 
} elseif ($race == "Tiznt") { 
    for ($i = 0; $i < $number; $i++) { 
    echo $first[$i] . $middle[$i] . $last[$i] . "<br />\n"; } 
} else {  
    for ($i = 0; $i < $number; $i++) { 
    echo $first[$i] . $last[$i] . "<br />\n"; } 
} 

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 
} 
?> 

如果我把生成的代码放到一个页面模板,然后使用该模板,表单显示正确地创建一个新的页面,而是当用户提交表单:

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

(文件namegen-page.php文件是我的模板文件,这是我的主题目录...看来它试图找到它在我的根目录。)

我需要有与发生器不同的页面形式?我是否需要使用Wordpress插件来显示PHP? 我对Wordpress相当陌生,但我不确定自己做错了什么。任何帮助表示赞赏!

回答

0

问题是,您将表单的action直接设置为您的模板文件(使用相对URL,这在任何情况下都不太可能),并且不会正确初始化WordPress。将其替换为使用该模板的页面的(绝对)永久链接。

+0

这工作完美,谢谢! – britne

0

有两个选项来实现这一点,我知道的:

  1. 您可以创建一个custom page template,其中包括与您希望现有的网页内容以及该代码。然后,你在创建页面时简单地选择这个模板,它应该显示并且工作得很好。
  2. 你可以尝试很多PHP plugins之一,虽然我没有 使用它们的经验。

我会推荐第一个选项,亲自。

我希望这有助于!

+0

我已经制作了自定义页面模板,但是我的表单操作是错误的,正如@ebohlman所说。不过谢谢你的额外资源! – britne

1

尝试将此代码移动到主题中的functions.php文件中,并将其设置为简码。短代码API位于here,但下面基本上是你需要做的。

这将涉及将所有代码包装在一个函数中,然后创建一个短代码。

在你的主题

//This is only to make sure another function doesn't already have that name 
    if (! function_exists('custom_name_generator')) { 
     function custom_name_generator(){ 
      //copy and paste your code here 
     } 
    } 
        //shortcode name,   reference to the function name 
    add_shortcode('custom_name_shortcode', 'custom_name_generator'); 

的functions.php文件现在在内容区域内的页面上的管理员,您可以将[custom_name_shortcode。 系统将选择已注册的短代码并执行您的代码。

希望有所帮助。祝你好运。

+0

我从来没想过这个!另一条评论解决了这个问题,但是感谢你提供关于创建短代码的信息,我可能会在另一天发现它的用处! – britne

相关问题