I am trying to create a Wordpress plugin that exports each blog post to my dropbox folder. The code is below but I have a problem.

If I run this code outside Wordpress, it works perfectly. The table is created and my token is stored. If I use a different browser it works perfectly...no authentication because it reads from the DB.

My problem is that when I put this into my plugin, the table is not created at all. So if I go to a different browser, I have to re-authenticate.

Help please.


 * Copyright 2012 Erin Dalzell.


include ABSPATH . '/wp-config.php';

function etd_initialize() {

global $current_user;

    // Set your consumer key, secret and callback URL
    // should be in the settings
    $key      = 'xxxxx';
    $secret   = 'yyyyy';
    $current_user = wp_get_current_user();

    // Check whether to use HTTPS and set the callback URL
    $protocol = (!empty($_SERVER['HTTPS'])) ? 'https' : 'http';
    $callback = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

    // Instantiate the Encrypter and storage objects
    $encrypter = new \Dropbox\OAuth\Storage\Encrypter('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

    // Instantiate the database data store and connect
    $storage = new \Dropbox\OAuth\Storage\PDO($encrypter, 1);
    $storage->connect(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);

    $storage = new \Dropbox\OAuth\Storage\Session($encrypter);

    $OAuth = new \Dropbox\OAuth\Consumer\Curl($key, $secret, $storage, $callback);

    $dropbox = new \Dropbox\API($OAuth);

    return $dropbox;


function export_to_dropbox($id) {
    $dropbox = etd_initialize();

    $post = get_post($id);

    // Create a temporary file and write some data to it
    $tmp = tempnam('/tmp','dropbox');
    file_put_contents($tmp, $post->post_content);

    // Upload the file with an alternative filename
    $put = $dropbox->putFile($tmp, $post->post_title . '.md');

    // Unlink the temporary file

register_activation_hook( __FILE__, 'etd_initialize' );
add_action('publish_post', 'export_to_dropbox');

I'm not familiar with the Dropbox API or their libraries, but most likely you'll need to write your own session handler to store the session data in your WP DB (or wherever you want, safely) and associate it with your user account. Essentially adusting this line: $storage = new \Dropbox\OAuth\Storage\Session($encrypter);.


If it's the WordPress side of things you are trying to figure out, you'll need to save your OAuth tokens in a usermeta field, then instead of doing the full authentication flow, first check that there isn't a usermeta value saved and use it if there is.

| improve this answer | |
  • Thanks, I have updated my question and the code. I am now supposed to be using the DB to store my token. It works outside of Wordpress, but not inside. Ideas? – emd Nov 17 '12 at 6:07
  • You'll have to store the token in the WP database and then add some code to check that it exists and retrieve it. This may require serializing and unserializing the token. – totels Nov 17 '12 at 17:11
  • Ya, that's what this Framework is supposed to do. Really frustrated that it works outside of Wordpress (table is created and token is stored) but not when run as a plugin (table not created). Why won't it create the table? I guess I'll have to modify the framework to expose the token and set it. Sigh – emd Nov 17 '12 at 17:47
  • Well I have no idea why it wasn't working before, but I got it to work by writing my own Storage class that saved the token in the WP Options table. – emd Nov 18 '12 at 0:28
  • Heh, yes, that's what I said in my first sentence. ;) – totels Nov 19 '12 at 23:40

I ended up writing my own Storage class that used the WordPress Options table to store the encrypted token.

| improve this answer | |

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.