Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This question already has an answer here:

This is my ipn.php that is sending a mail to the user when PayPal transaction is completed.

The problem is that I also need to change the state of the order in the WP database and not just send the email.

I know the unique $code variable (integer) of that transaction (I have saved it when the form was sent and the user redirected to finish the process on PayPal page) and PayPal is sending it back to ipn.php file inside custom field $_POST['custom'].

So, the code in ipn.php (accessed by PayPal as notify_url when the transaction is successful) looks like this:

<?php

$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}

// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

// CUSTOM VARIABLE
$code = $_POST['custom'];

if (!$fp) {
    // ERROR
} else {

    $email = "emailtouser@email.com" ;
    $subject = "Test" ;
    $message = "Your code is {$code}" ;
    mail($email, $subject, $message, "From:" . $email);

    fclose ($fp);
}

So, the email is send OK with the code value as it supposed to be.

However, I also need to get the user email from the WP database based on the $code that is unique for every user (post type) and then change the state of the order in the db.

I know how to do that, but the problem is that WP is not loaded and I cannot access the database to perform these acts.

How to do that?

I mean, how to access WP db here ?:

} else {

        // BEFORE SENDING I NEED TO ACCESS WP DB HERE...


        // AND THEN SEND THE EMAIL
        $email = "emailtouser@email.com" ;
        $subject = "Test" ;
        $message = "Your code is {$code}" ;
        mail($email, $subject, $message, "From:" . $email);

        fclose ($fp);
    }
share|improve this question

marked as duplicate by t f, s_ha_dum, toscho Nov 12 '13 at 12:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
If you can't access the database, then how are you going to get any data from it? –  anu Nov 7 '13 at 10:15
    
@anu I am getting no data from it, you are right. I need to connect to the WP db somehow from the ipn.php file, that is accessed by PayPal (I have specified it in notify_url). Have you worked with PayPal and WP before? –  Derfder Nov 7 '13 at 10:50
    
@anu I need to know how to access WP db from ipn.php file (wp is not loaded) –  Derfder Nov 7 '13 at 11:03
    
It's a MySQL database. You can connect to MySQL databases with PHP. –  anu Nov 7 '13 at 11:07
    
@anu So, it's impossible with WP? –  Derfder Nov 7 '13 at 11:10

1 Answer 1

Try adding this to the top of your script:

require('/path/to/httpdocs/wp-blog-header.php'); 
global $wpdb;

Then you can run queries using:

$sql = "Your SQL Query Here";
$wpdb->get_results($sql)
share|improve this answer

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