我正在尝试创建一个Wordpress插件,将每个博客文章导出到我的dropbox文件夹。代码如下,但我有一个问题。
如果我在Wordpress之外运行这段代码,它会工作得很好。创建表并存储我的令牌。如果我使用不同的浏览器,它可以完美地工作。。。没有身份验证,因为它从DB读取。
我的问题是,当我将其放入插件时,根本没有创建表。因此,如果我转到其他浏览器,我必须重新验证。
请帮忙。
代码:
/*
* Copyright 2012 Erin Dalzell.
*
*/
require_once(\'Dropbox/API.php\');
require_once(\'Dropbox/Exception.php\');
require_once(\'Dropbox/OAuth/Consumer/ConsumerAbstract.php\');
require_once(\'Dropbox/OAuth/Consumer/Curl.php\');
require_once(\'Dropbox/OAuth/Storage/Encrypter.php\');
require_once(\'Dropbox/OAuth/Storage/StorageInterface.php\');
require_once(\'Dropbox/OAuth/Storage/Session.php\');
require_once(\'Dropbox/OAuth/Storage/Filesystem.php\');
require_once(\'Dropbox/OAuth/Storage/PDO.php\');
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
unlink($tmp);
}
register_activation_hook( __FILE__, \'etd_initialize\' );
add_action(\'publish_post\', \'export_to_dropbox\');
?>