0

I built my own contact form (no plugins) and want to have a secure way to get an emailadress from my option page. get_option and other built in WP stuff doesn't work from within my process.php file.

I tried to make a query to WP-Database like that:

$query  = "SELECT * FROM wp_options WHERE option_name = 'nothing_settings_contact' LIMIT 1";
$sender = mysql_query($query) or die(mysql_error());

But that also doesn work. (And if it would, I would just get an IP, with which I dont know what to do)

So: How do I get this emailadress in there, without exposing it to the users?

Edit: This is my process.php. I tried to require wp-blog-header, but it breaks 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('Sebastian.Starke1@googlemail.com',"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']);
        }
    }
5
  • This question is not directly related to WordPress, you should try googling for PHP+MySQL interaction.
    – gyo
    May 7, 2013 at 12:50
  • Is there no WP-way of doing this? May 7, 2013 at 12:51
  • I spent the whole day googling this kind of stuff, and it didnt help me. Im not shure, if Im on the right track with my sollution. May 7, 2013 at 12:53
  • You stated that you're not in the context of WP because you can't use get_option etc... otherwise that's the WP-way.
    – gyo
    May 7, 2013 at 12:58
  • @Lord_Mord - I am assuming the reason you can't run the query is because you don't have access to the database information from the process.php file. You could load wordpress into that file first, which would make all WP functions available. You can load wordpress with a php require statement: require_once("/path/to/wordpress/wp-load.php"); It would help if you could post the errors you are getting, or the code of the process.php page.
    – gdaniel
    May 7, 2013 at 13:03

1 Answer 1

1

Add this at the start of your process.php file to get access to get_option and other wordpress functions:

define( 'WP_USE_THEMES', false );  
require('wp-blog-header.php'); 

You might have to change the path to wp-blog-header.php.

3
  • That doesn't work. If I try a relative url, the contact form just loads (The data is being send to process.php, but no success or error. I receive no email). If I try a fixed url the form sends, throws back a success message, but I still receive no email. I will post process.php above. May 7, 2013 at 15:54
  • Same thing with wp-load.php May 7, 2013 at 15:58
  • You shouldn't require http://www.bastards-design.de/sebastian/wp-blog-header.php. You should pass the path relative to the files you are using. Read include, it works in the same way as require
    – RRikesh
    May 8, 2013 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.