Sebastian Starke
Sebastian Starke

Reputation: 5337

Require wp-blog-header.php breaks PHP

I'm trying to require wp-blog-header.php from within the process.php of my custom built contact form. But if I do so, the message isn't sent anymore. I tried it with fixed and with relative path. The only difference is, that with fixed url, success message is returned, but the message doesnt reach me anyway.This is process.php:

    <?php if( isset($_POST) ){

        require('http://www.bastards-design.de/sebastian/wp-blog-header.php');

        //form validation vars
        $formok = true;
        $errors = array();

        //sumbission data
        $ipaddress = $_SERVER['REMOTE_ADDR'];
        $date = date('d.m.Y');
        $time = date('H:i');

        //form data
        $name = $_POST['name']; 
        $email = $_POST['email'];
        $website = $_POST['website'];
        $enquiry = $_POST['enquiry'];
        $message = $_POST['message'];

        $receiver = $_POST['receiver'];
        $sender = "Niemand";// get_option('admin_email');

        if(empty($name)){
            $formok = false;
            $errors[] = "Sie haben keinen Namen angegeben.";
        }

        if(empty($email)){
            $formok = false;
            $errors[] = "Sie haben keine Emailadresse angegeben.";
        //validate email address
        }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $formok = false;
            $errors[] = "Sie haben keine gültige Emailadresse angegeben.";
        }

        if(empty($message)){
            $formok = false;
            $errors[] = "Das Nachrichtenfeld ist leer.";
        }
        elseif(strlen($message) < 20){
            $formok = false;
            $errors[] = "Ihre Nachricht muss mindestens 20 Zeichen enthalten.";
        }

        if($formok){
            $headers = "From: {$email}" . "\r\n";
            $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

            $emailbody = "<p><strong>Name: </strong> {$name} </p>
                          <p><strong>Website: </strong> {$website} </p>
                          <p><strong>Nachricht: </strong> {$message} </p>
                          <p>Diese Nachricht wurde am {$date} um {$time} über {$sender} gesendet.</p>";

            if($receiver){
                mail($receiver,"Anfrage ".$name,$emailbody,$headers);
            }
            else{
                mail('[email protected]',"Error",$emailbody,$headers);
            }
        }

        //what we need to return back to our form
        $returndata = array(
            'posted_form_data' => array(
                'name' => $name,
                'email' => $email,
                'website' => $website,
                'enquiry' => $enquiry,
                'message' => $message
            ),
            'form_ok' => $formok,
            'errors' => $errors
        );

        //if this is not an ajax request
        if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
            //set session variables
            session_start();
            $_SESSION['cf_returndata'] = $returndata;

            //redirect back to form
            header('location: ' . $_SERVER['HTTP_REFERER']);
        }
    }

Upvotes: 0

Views: 4400

Answers (2)

T.Todua
T.Todua

Reputation: 56487

BTW, if you want to include worpdress core (+theme functions), its better to user:

require('/wp-load.php');

and NOT

require('/wp-blog-header.php');

Upvotes: 2

Rolando Isidoro
Rolando Isidoro

Reputation: 5114

PHP include and require include and evaluate a file for the specified path.

Both expect a path on the server and not a URL. When you pass http://www.bastards-design.de/sebastian/wp-blog-header.php it will only include the output of the script and not evaluate the PHP code as you want.

Assuming that the code you're showing is from a file located in your server in /var/www/bastards-design/script.php and the file you want to include is at /var/www/bastards-design/sebastian/wp-blog-header.php here's how you can solve your problem:

  • Use absolute path on your server - ex:. require '/var/www/bastards-design/sebastian/wp-blog-header.php';
  • Use relative path - ex:. require 'sebastian/wp-blog-header.php';

Upvotes: 2

Related Questions