WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


$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);

$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 {


        $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 tfrommen, 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.

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

Try adding this to the top of your script:

global $wpdb;

Then you can run queries using:

$sql = "Your SQL Query Here";
share|improve this answer

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