0

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?

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
Stephen Rose
  • 1
  • 1
  • 3

1 Answers1

1

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]
Slico
  • 436
  • 2
  • 16
  • I am trying that with the following code: Thank you for your form.

    You stated that you are $age years old and $supplement_user use supplements.

    "; ?>
    – Stephen Rose Oct 05 '16 at 15:59
  • 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 Oct 05 '16 at 16:00
  • Why do I get this 'undefined function get_header()' error? – Stephen Rose Oct 05 '16 at 16:13
  • 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? – Stephen Rose Oct 05 '16 at 16:30
  • Inside your theme http://localhost/wordpress/wp-content/themes/YOUR_THEME/ – Slico Oct 05 '16 at 16:39
  • Are you trying to run a Wordpress theme outside of an installation of Wordpress? – Slico Oct 05 '16 at 16:42
  • My php file is located here: `public_html/whatsuppnews.com/wp-content/themes/Avada` and Avada is the theme I am using, so this seems like the correct place. The php does execute without the ****, but when I add that function, it errors. – Stephen Rose Oct 05 '16 at 17:26
  • It is running on Wordpress – Stephen Rose Oct 05 '16 at 18:24
  • This is at the top of my php script in the following directory: **public_html/whatsuppnews.com/wp-content/themes/Avada** – Stephen Rose Oct 06 '16 at 03:50
  • Look at the updated answer. You need at least a header.php file. – Slico Oct 06 '16 at 08:42
  • header.php is located in the same directory as my php script: **public_html/whatsuppnews.com/wp-content/themes/Avada** – Stephen Rose Oct 06 '16 at 13:51
  • I don't know where to help you. Can you check if you have the functions in /wordpress/wp-includes/general-template.php? – Slico Oct 06 '16 at 14:19
  • yes, but I have 2 wp-includes, one right under public_html and one under my sitename. Both wp-includes folders have a functions.php file and general-template.php file. – Stephen Rose Oct 06 '16 at 15:24
  • You need only the one inside WordPress directory not your theme root – Slico Oct 06 '16 at 16:22
  • Technically, this is the correct answer. It sounds like you have problems with your Wordpress installation if your theme template file cannot find the function `get_header()`. – Lee Oct 07 '16 at 16:03
  • I've just noticed your path... should you have `whatsuppnews.com` inside your `public_html` directory...? If you have two `wp-includes` folders, then I think you have actually put two installations into the same folder. Inside your `public_html` folder should only have three folders: `wp-admin`, `wp-content` and `wp-includes` This is your Wordpress installation. – Lee Oct 07 '16 at 16:04
  • Copy everything inside your `whatsuppnews.com` folder, and paste inside your `public_html` folder, then delete your `whatsuppnews.com` folder. That should sort your Wordpress installation back to how it should be. Back up first! – Lee Oct 07 '16 at 16:07
  • Ok, I did that. The website is still operational. Inside of public_htlml, I have 4 folders: wp-admin, wp-content and wp-includes and cgi-bin. I have tried running the php file with this command - wp-admin, wp-content, wp-includes, and cgi-bin. I have created my php file with this code: Then I have put that php file inside of public_html folder and I get the undefined function error. I also put the php file inside the theme folder and I still get an undefined function error. – Stephen Rose Oct 14 '16 at 20:11
  • Ignore the above comment. – Stephen Rose Oct 14 '16 at 20:12
  • Ok, I did that. The website is still operational. Inside of public_htlml, I have 4 folders: wp-admin, wp-content and wp-includes and cgi-bin. I created a php file with this command: Then I have put that php file inside of public_html folder and I get the undefined function error. I also put the php file inside the theme folder and I still get an undefined function error. – Stephen Rose Oct 14 '16 at 20:14
  • I updated my answer again, please @StephenRose read carefully the wordpress documentation, this is so easy to solve and we are creating a big conversation in the comments section. If you need, download wordpress again, start a clean installation and check inside themes/ directory the built-in templates of wordpress and check the correct project structure and the correct use of get_header() function – Slico Oct 17 '16 at 14:17