简体   繁体   English

将自定义PHP添加到Wordpress页面

[英]Adding custom PHP to Wordpress page

I have a small name generator script I am trying to add to my Wordpress website. 我有一个小的名称生成器脚本,正在尝试添加到我的Wordpress网站中。 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 ) (这是一个现场演示: 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
}
?>

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 致命错误:在第7行的/home/content/09/10184409/html/namegen-page.php中调用未定义的函数get_header()

(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.) (文件名gen-page.php是我的模板文件,位于我的主题目录中...似乎正在尝试在我的根目录中找到它。)

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? 我需要使用Wordpress插件来显示PHP吗? I'm fairly new to Wordpress, and I'm not sure what I'm doing wrong. 我刚接触Wordpress,但不确定自己做错了什么。 Any help is appreciated! 任何帮助表示赞赏!

Try moving this code into the functions.php file in your theme and setting is up as a shortcode. 尝试将此代码移至主题中的functions.php文件中,并将设置设置为简码。 The shortcode api is located here , but below is basically what you'll need to do. 简码api位于此处 ,但基本上是您需要做的。

This will involve wrapping the all of the code in a function and then creating a short code. 这将涉及将所有代码包装在一个函数中,然后创建一个短代码。

In the functions.php file of your theme 在您主题的functions.php文件中

    //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' );

Now in the admin on a page within the content area, you can place [custom_name_shortcode]. 现在,在内容区域内页面上的管理员中,您可以放置​​[custom_name_shortcode]。 The system will pick on on the registered shortcode and exec your code. 系统将选择已注册的简码并执行您的代码。

Hope that helps. 希望能有所帮助。 Good luck. 祝好运。

There are two options to accomplish this that I know of: 我知道有两种选择可以实现此目的:

  1. You can create a custom page template that includes this code along with the existing page content that you'd like. 您可以创建一个自定义页面模板 ,其中包含此代码以及所需的现有页面内容。 Then, you simple select this template when creating the page and it should appear and work just fine. 然后,您只需在创建页面时选择此模板,它就会出现并正常工作。
  2. You can try one of many PHP plugins , although I have no experience using them. 您可以尝试许多PHP插件之一 ,尽管我没有使用它们的经验。

I would recommend the first option, personally. 我个人会推荐第一种选择。

I hope this helps! 我希望这有帮助!

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. 问题是您将表单的action直接设置为模板文件(使用相对URL,无论如何都不太可能),并且无法正确初始化WordPress。 Replace it with the (absolute) permalink to the page that uses the template. 将其替换为使用该模板的页面的(绝对)永久链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM