【问题标题】:Adding custom PHP to Wordpress page将自定义 PHP 添加到 Wordpress 页面
【发布时间】:2013-05-29 18:24:27
【问题描述】:

我有一个小名称生成器脚本,我正在尝试添加到我的 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
}
?>

如果我将生成器代码放入页面模板,然后使用模板创建新页面,表单会正确显示,但是当用户提交表单时:

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

(文件名gen-page.php 是我的模板文件,它在我的主题目录中......它似乎试图在我的根目录中找到它。)

我是否需要将表单与生成器放在不同的页面上?我是否需要使用 Wordpress 插件来显示 PHP? 我对 Wordpress 还很陌生,我不确定自己做错了什么。任何帮助表示赞赏!

【问题讨论】:

    标签: php wordpress forms templates


    【解决方案1】:

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

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

    在你的主题的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' );
    

    现在在内容区域内的页面管理中,您可以放置​​ [custom_name_shortcode]。 系统将选择注册的短代码并执行您的代码。

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

    【讨论】:

    • 我从来没有想过这个!另一条评论解决了这个问题,但感谢您提供有关创建短代码的信息,我可能会在另一天找到它的用途!
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      据我所知,有两种选择可以实现:

      1. 您可以创建一个custom page template,其中包含此代码以及您想要的现有页面内容。然后,您只需在创建页面时选择此模板,它就会显示出来并正常工作。
      2. 您可以尝试众多PHP plugins 之一,虽然我没有 体验使用它们。

      我个人会推荐第一个选项。

      我希望这会有所帮助!

      【讨论】:

      • 我确实已经制作了自定义页面模板,但正如@ebohlman 所说,我的表单操作是错误的。感谢您提供额外的资源!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 2015-03-30
      • 1970-01-01
      相关资源
      最近更新 更多