0

Hy all! From what I read, the best way to add PHP code to a wordpress page would be via a custom template. So i took the page.php from the theme and customised it with html code without any problems. The problem is with the PHP code. No matter where I add it, it doesn't work.

My question is where do I add the custom PHP code for the form validation?

The page looks like this:

/*
Template Name: example
*/

<?php
get_header();
if ($tempera_frontpage=="Enable" && is_front_page()): get_template_part( 'frontpage' );
else :
?>

<section id="container" class="<?php echo tempera_get_layout_class(); ?>">
<div id="content" role="main">
<?php cryout_before_content_hook(); ?>
<?php get_template_part( 'content/content', 'page'); ?>

I added the HTML code here:  

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> " >
.....
</form>

<?php
endif;
get_footer();
?>

3 Answers3

0

According to this, you will add it before the HTML, near the top of your code.

Alicia Uhacz
  • 77
  • 6
  • 20
0

Well basically it's PHP and should work, if you want to validate the form in the same template file, use an empty action attribute:

<form method="post" action="">
.....
</form>

And make sure not to use an WordPress reserved word on your form element name attribute, for example name.

At the top of the file you can verify and validate all the request being sent by the form as any other PHP file.

<?php
if (!empty($_POST)) {
  //validate or do something here
}
get_header();
if ($tempera_frontpage=="Enable" && is_front_page()): get_template_part( 'frontpage' );
else :
?>

Hope this solution help you out.

Welling
  • 556
  • 3
  • 9
  • Thank you very much! This was an easy solution and it works. One more question, is there any way to incorporate the original action code? From what i read it's preety important for the form validation – Cristi Tudor Aug 02 '14 at 07:05
  • You can still add the current url to the action, for example: `
    ` or look for a way to use the FULL url here: http://stackoverflow.com/questions/6768793/get-the-full-url-in-php
    – Welling Aug 03 '14 at 07:40
0

I'm thinking you should be modifying the content-page.php template. Note the page template calls it with:

<?php get_template_part( 'content/content', 'page'); ?>

Within the content template you should see something like

<?php the_content(); ?>

If you add the form html on the next line, it will be within the same div, something like

<div class="entry-content>
</div>

This way the new content will fall within the same container as other content and be styled as such.

heytricia
  • 298
  • 1
  • 3
  • 12