I am trying to create a custom form in WordPress. Step 1 of the form is HTML code that collects data and sends it to a PHP file through the post method, then writes it to the MySQL database and creates Step 2 of the form using PHP code. My problem is that I want to include the default WordPress header and footer in Step 2 of the form that WordPress uses in Step 1. Is there a way to do this by including the code of header.php and footer.php in my PHP script?
1 Answer
You mean this?
<?php get_header(); ?>
And for the footer:
<?php get_footer(); ?>
If you take a look at the get_header() function:
function get_header( $name = null ) {
/**
* Fires before the header template file is loaded.
*
* The hook allows a specific header template file to be used in place of the
* default header template file. If your file is called header-new.php,
* you would specify the filename in the hook as get_header( 'new' ).
*
* @since 2.1.0
* @since 2.8.0 $name parameter added.
*
* @param string $name Name of the specific header file to use.
*/
do_action( 'get_header', $name );
$templates = array();
$name = (string) $name;
if ( '' !== $name ) {
$templates[] = "header-{$name}.php";
}
$templates[] = 'header.php';
locate_template( $templates, true );
}
Do you have at least a header.php file on your root theme?
A common Wordpress structure:
your_project_folder
-wp-admin
-wp-content
-languages
-plugins
-themes
-YOUR_THEME_FOLDER
-[HERE YOU PLACE YOUR index.php file and header.php for example, and inside index.php you place your get_header() function]
-upgrade
-uploads
-wp-includes
index.php
wp-activate.php
wp-blog-header.php
wp-comments-post.php
wp-config.php
wp-cron.php
[...more files]
21 Comments
Stephen Rose
I am trying that with the following code: <?php get_header(); ?> <?php // Script 3.5 - handle_form.php #3 ini_set('display_errors', 1); // Let me learn from my mistakes! error_reporting(E_ALL); // Show all possible problems! // Create shorthand versions of the variables: $age = $_POST['age']; $sex = $_POST['sex']; $supplement_user = $_POST['supplement_user']; // Print the received data: print "<p>Thank you for your form.</p> <p>You stated that you are $age years old and $supplement_user use supplements.</p>"; ?>
Stephen Rose
This is the error I get: Fatal error: Uncaught Error: Call to undefined function get_header() in /home/whatsu13/public_html/whatsuppnews.com/wp-content/themes/Avada/handle_form.php:2 Stack trace: #0 {main} thrown in /home/whatsu13/public_html/whatsuppnews.com/wp-content/themes/Avada/handle_form.php on line 2
Stephen Rose
Why do I get this 'undefined function get_header()' error?
Stephen Rose
When you reference a function like get_header(), is there a particular directory you need to be in for Wordpress to recognize the function call?
Slico
Inside your theme localhost/wordpress/wp-content/themes/YOUR_THEME
|