I'm building an application on WordPress wherein I have a single form with two entries:
1) content of the tweet
2) time at which the tweet should be posted on the user's Twitter timeline
Form:
<form method = "POST">
<input type = "text" name = "tweet1">
<input type = "datetime-local" name = "date1">
<input type = "submit">
</form>
Now, on submitting the form, I want to post the content of the text field to be posted on the user's Twitter timeline at the time specified by the user in the form. $_SESSION['x']
and $_SESSION['y']
stores the user's oauth_token
and oauth_token_secret
respectively.
<?php
if(isset($_POST['submitted']))
{
function do_this_one()
{
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['x'], $_SESSION['y']);
$response = $connection->post("statuses/update", array('status' => $_POST['tweet1']));
}
date_default_timezone_set('Asia/Calcutta');
$myDate = strtotime($_POST['date1']);
$now = time();
$ti_one = $myDate - $now;
echo time() + $ti_one;
add_action( 'my_new_event','do_this_one' );
wp_schedule_single_event(time() + $ti_one, 'my_new_event' );
?>
$ti_one
variable basically stores the time in seconds between now and the time specified by the user. For example, if the current time is 2:00 PM, then if the time specified by the user in the form is 2:03 PM, then $ti_one
would be equal to 180 seconds. That's the reason why I'm scheduling the event at time() + $ti_one
in:
wp_schedule_single_event(time() + $ti_one, 'my_new_event' );
However, on submitting the form, the tweet is not being posted at the specified time. The above code ie code for the form and the PHP code is inside callback.php
, which is the page that the users are redirected to after granting permission to the OAuth client.
What seems to be wrong with my code? Am I scheduling the event incorrectly?