2013-05-29 225 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

相關問題