我正在尝试创建一个基本应用程序,允许用户安排在用户指定时间在其时间轴上发布多条推文。基本上,这将是应用程序的流程:
3)用户被重定向到表单,他们可以通过单击Schedule a tweet
添加新的消息字段。然后,他们可以填写推文的内容,以及他们希望在他们的时间轴上发布推文的相应时间。
我正在使用亚伯拉罕的twitteroauth
在我的应用程序中实现Twitter OAuth。
负责在成功登录后重定向用户时显示表单的模板的源代码:
<pre>
<?php
/*
*Template Name: Callback
*/
?>
<?php
session_start();
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
define('CONSUMER_KEY', "XXXXXXXXXXXXXXXXX");
define('CONSUMER_SECRET', "XXXXXXXXXXXXXXXXXXXXXXXXX");
define('OAUTH_CALLBACK', " http://localhost/wordpress/index.php/callback/");
$request_token = [];
$request_token['oauth_token'] = $_SESSION['oauth_token'];
$request_token['oauth_token_secret'] = $_SESSION['oauth_token_secret'];
if (isset($_REQUEST['oauth_token']) && $request_token['oauth_token'] !== $_REQUEST['oauth_token'])
{
echo "Opps! Something went wrong!";
}
else
{
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $request_token['oauth_token'], $request_token['oauth_token_secret']);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier']));
//print_r($access_token);
$_SESSION['access_token'] = $access_token;
}
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
//$user = $connection->get("account/verify_credentials");
//$response = $connection->post("statuses/update", array('status' => 'fsociety'))
//$response = $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/update'), array(
//'status' => 'Conceit to fall on parasol.'
?>
<script>
var count = 0
function addNewMessage(count)
{
if(count > 5)
{
window.alert("NO MORE THAN 5!");
}
else
{
var celeb = document.createElement("input");
celeb.type = "text";
celeb.name = "tweet" + count;
celeb.placeholder = "Tweet" + " " + count;
celebrity.appendChild(celeb);
var date = document.createElement("input");
date.type = "datetime-local";
date.name = "date" + count;
date.placeholder = "message-date" + " " + count;
celebrity.appendChild(date);
celebrity.appendChild(document.createElement("br"));
celebrity.appendChild(document.createElement("br"));
}
}
</script>
<form method = "POST" action = "">
<fieldset>
<a style = "color:red" onclick = "addNewMessage(++count)">Schedule a tweet</a>
<div id = "celebrity"/>
</fieldset>
<br>
<fieldset>
<input type="hidden" name="submitted" id="submitted" value="true" />
<?php wp_nonce_field( 'post_nonce', 'post_nonce_field' ); ?>
<button type="submit"><?php _e('Add Campaign', 'framework') ?></button>
</fieldset>
</form>
</pre>
现在,正如上面的源代码中所评论的那样,我可以通过以下方式将推文发布到用户的时间轴:
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$response = $connection->post("statuses/update", array('status' => 'fsociety'))
现在,我的问题是,如何通过从表单字段中提取推文内容和时间信息,在同一模板中使用cron作业在用户定义的时间安排用户定义的推文?
PS:我知道如何使用插件实现cron作业。但是,由于我的cron作业依赖于表单字段数据,我想在上面的模板文件中编写用于调度此cron作业的代码,而不是在自定义插件中。
编辑:
我创建了一个自定义的帖子类型,它将表单数据存储在wp-postmeta
表中。