简体   繁体   中英

Adding custom PHP to Wordpress page

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("\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

(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!

Try moving this code into the functions.php file in your theme and setting is up as a shortcode. The shortcode api is located here , but below is basically what you'll need to do.

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

    //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]. 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.

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. Replace it with the (absolute) permalink to the page that uses the template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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