0

I am trying to display a php form in wordpress. I was given the following code by default (which indicated that it can be used to display custom fields) and edited it but it is still not displaying the form :( I would like to figure out how to easily display this as I need to add custom fields and am very new to PHP.

/*
 * The form for adding custom fields on the options page.
 */
function fcpetition_fieldform($po) {
    ?>
            <form method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>"/>
                <input type="radio" name="addfield" value="yes"/>
                <input type="dropdown" name="petition_select" value="<?php echo $po; ?>"/>
                <input type="dropdown" name="editpetition" value="<?php print $po;?>">
                Type: <select name = "fieldtype">
                        <option value="text">Text box</option>
                        <option value="select">Drop down box</option>
                      </select>
                Name:<input type="text" name="fieldname"/>
                Options:<input type="text" name="options"/>
                Publish field <input type="checkbox" name="hide" checked/>
                <input type="submit" name="Submit" value="<?php _e("Add","fcpetition")?>"/>
            </form>
    <?php
}

Original code w/o my edits:

 /*
     * The form for adding custom fields on the options page.
     */
    function fcpetition_fieldform($po) {
        ?>
                <form method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>"/>
                    <input type="hidden" name="addfield" value="yes"/>
                    <input type="hidden" name="petition_select" value="<?php echo $po; ?>"/>
                    <input type="hidden" name="editpetition" value="<?php print $po;?>">
                    Type: <select name = "fieldtype">
                            <option value="text">Text box</option>
                            <option value="select">Drop down box</option>
                          </select>
                    Name:<input type="text" name="fieldname"/>
                    Options:<input type="text" name="options"/>
                    Publish field <input type="checkbox" name="hide" checked/>
                    <input type="submit" name="Submit" value="<?php _e("Add","fcpetition")?>"/>
                </form>
        <?php
    }

2 Answers2

0

I am not really one for putting HTML but I guess that's the wordpress definition of how it's supposed to be. If i had to however, I would use a heredoc;

function echoForm() {
    $html = <<<FORM
    // html code here
    FORM;

    echo $html;
    // or
    return $html;
}
Community
  • 1
  • 1
Peter
  • 8,776
  • 6
  • 62
  • 95
0

This function should outputs/prints an HTML form, but it can't work in your case, because the HTML is written in a PHP function. The PHP function should return/echo/print the content. You can also use a heredoc.

You need to add this function in the "function.php" file of your theme's folder.

I really don't understand what you're doing in the "action" attribute of the <form> tag. You should be pointing to an "action file" which would handle this data.

Where you should put the "action file" ?

  • some people create an action file in the "wp-content" folder.
  • some people create the action file at the root of the Wordpress folder.

I usually create an "action.php" file at the root of the Wordpress folder and I use the following: <form action="action.php" method="post">...</form>.

You shouldn't put the action file in the theme's folder, because it's not related to a specific theme, and it shouldn't be updated by the Wordpress Updates feature.

Wissam El-Kik
  • 2,469
  • 1
  • 17
  • 21